Get value (String) of ArrayList<ArrayList<String>>(); in Java
android, arraylist, java
Solution
listOfSomething.Clear();
listOfSomething.Add("first");
collection.Add(listOfSomething);
You are clearing the list here and adding one element ("first"), the 1st reference of `listOfSomething` is updated as well sonce both reference the same object, so when you access the second element `myList.get(1)` (which does not exist anymore) you get the null.
Notice both `collection.Add(listOfSomething);` save two references to the same arraylist object.
You need to create two different instances for two elements:
ArrayList<ArrayList<String>> collection = new ArrayList<ArrayList<String>>();
ArrayList<String> listOfSomething1 = new ArrayList<String>();
listOfSomething1.Add("first");
listOfSomething1.Add("second");
ArrayList<String> listOfSomething2 = new ArrayList<String>();
listOfSomething2.Add("first");
collection.Add(listOfSomething1);
collection.Add(listOfSomething2);
Problem
I know it's simple question, but in ``` ArrayList<ArrayList<String>> collection; ArrayList<String> listOfSomething; collection= new ArrayList<ArrayList<String>>(); listOfSomething = new ArrayList<String>(); listOfSomething.Add("first"); listOfSomething.Add("second"); collection.Add(listOfSomething); listOfSomething.Clear(); listOfSomething.Add("first"); collection.Add(listOfSomething); ``` I want to take String from ArrayList of ArrayList, and I don't know how to do that. For example I go ``` ArrayList<String> myList = collection.get(0); String s = myList.get(0); ``` and it works! but: Big update: ``` private List<S> valuesS; private List<Z> valuesZ; ArrayList<ArrayList<String>> listOfS; ArrayList<String> listOfZ; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Zdatasource = new ZDataSource(this); Zdatasource.open(); valuesZ = Zdatasource.getAllZ(); Sdatasource = new SDataSource(this); Sdatasource.open(); valuesS = Sdatasource.getAllS(); List<Map<String, String>> groupData = new ArrayList<Map<String, String>>(); List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>(); listOfS = new ArrayList<ArrayList<String>>(); listOfZ = new ArrayList<String>(); for (S i : valuesS) { // S is class for (Z j : valuesZ) { // Z is class if(j.getNumerS().equals(i.getNumerS())) { listOfZ.add(j.getNumerZ()); } else { //listOfZ.add("nothing"); } } listOfS.add(listOfZ); if(!listOf.isEmpty()) listOfZ.clear(); } @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { try { ArrayList<String> myList = listOfS.get(groupPosition); String s = myList.get(childPosition); PrintToast("group "+Integer.toString(groupPosition)+", child "+Integer.toString(childPosition) + " , "+ s); } catch(Exception e) { Log.e("FS", e.toString()); } return true; } ``` return me java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0 when I click on item which really should exist. I didn't show code which generate ListView, but I can tell you that my listOfS contains 3 items: first is Null listOfZ, second listOfZ got 2 elements, third listOfZ got 1 element.