0

I'm having an issue splitting a string array into a List/ArrayList. I realise this is basic stuff but I've looked at so many examples that I've now completely confused myself at what's happening (or not happening).

Some code snippets:

private String[] stringTempList;
private ArrayList<List<String>> arrayImageList = new ArrayList<List<String>>();

My list is read from a webpage and is formatted in plain text like: ['One','Two','Three', etc ...]

So, some lines to strip out the stuff I want/don't want (note - I've separated these out to help me follow the process through):

stringExtractedList = stringText.substring(stringText.indexOf("['") + 2,
                stringText.lastIndexOf("']"));    
stringTempList = stringExtractedList.split("','");

From what I can see the above works as expected (creating an array (stringTempList), splitting out each item where it sees ','.

Where it's going wrong:

arrayImageList.add((List<String>) Arrays.asList(stringTempList));

I expect this line to take my array (stringTempList) and move the items into an ArrayList. I was hoping to use code similar to arrayImageList.get(i); to access individual elements.

However, the code above seems to add all items to the first index in arrayImageList (list size is always 1). Running some debug tests eg Log.d("Test", arrayImageList.get(0)); returns the following:

[One,Two,Three,Four, etc...] 

I'd be grateful if someone could point me in the right direction. I think I've confused two different ideas here.

3
  • Did you check the exact result of the split(",")? Commented May 21, 2012 at 19:25
  • Not familiar with Java, but private ArrayList<List<String>> arrayImageList = new ArrayList<List<String>>(); creates a list of lists. What you want is a list only. So pick either ArrayList or List<String>. At that point it is redundant to create a new list - so just take the result of split directly. Or convert it to the desired type (most likely creating a copy in that step) Commented May 21, 2012 at 19:30
  • Do you want to create lists of lists with parsed elements from the string? arrayImageList.add((List<String>) Arrays.asList(stringTempList)); is not valid java code. Commented May 21, 2012 at 19:34

4 Answers 4

2

Change arrayImageList to a list of strings:

private ArrayList<String> arrayImageList = new ArrayList<String>();

and add them using the addAll method:

arrayImageList.addAll((List<String>) Arrays.asList(stringTempList));
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. In my error correcting previous errors, I stumbled on the ArrayList<List<String>> solution which just confused me. Simple solution here.
2

I think you want to use addAll() instead of add()

You'll also want to declare

private List<String> arrayImageList = new ArrayList<String>();

2 Comments

No problem! Does the reasoning behind this make sense to you?
Yeah, I think so. I'm very new to Java (and OOP in general) so applying my previous coding knowledge doesn't always help. In my research (trying to debug my issue) I found that an ArrayList is an implementation of a List. I also read that List should be used in preference of ArrayList where possible. In my case - initially I expected the array to be fixed length (so List would have done) but as I plan further - I suspect a non-fixed length list is going to be beneficial (eg removing broken image links).
1

You want to use addAll, not add.

1 Comment

Thanks. I think part of the confusion was that .add seemed to be adding all the elements - just not as expected.
0

I'm not sure i got this "wrong the right way", and I'm not that familiar with java, but here are my thoughts:

Your "arrayImageList" object is an ArrayList of List's. When youre adding your stringTempList-object, that object is correctly stored at the first available index of your 'List-of-lists'. If you want to access the individual elements (strings) from your "arrayImageList", you either have to adress the second dimension (the list of the list) like this:

arrayImageList.get(0).get(i) <- not entirely sure about java-syntax here, but you get the picture.

If on the other hand you dont need the extra dimension, just use ArrayList instead og ArrayList>.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.