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.
private ArrayList<List<String>> arrayImageList = new ArrayList<List<String>>();creates a list of lists. What you want is a list only. So pick eitherArrayListorList<String>. At that point it is redundant to create a new list - so just take the result ofsplitdirectly. Or convert it to the desired type (most likely creating a copy in that step)