[java] Get value (String) of ArrayList<ArrayList<String>>(); in Java

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.

This question is related to java android arraylist

The answer is


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);

Because the second element is null after you clear the list.

Use:

String s = myList.get(0);

And remember, index 0 is the first element.


The right way to iterate on a list inside list is:

//iterate on the general list
for(int i = 0 ; i < collection.size() ; i++) {
    ArrayList<String> currentList = collection.get(i);
    //now iterate on the current list
    for (int j = 0; j < currentList.size(); j++) {
        String s = currentList.get(1);
    }
}

A cleaner way of iterating the lists is:

// initialise the collection
collection = new ArrayList<ArrayList<String>>();
// iterate
for (ArrayList<String> innerList : collection) {
    for (String string : innerList) {
        // do stuff with string
    }
}

Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to arraylist

Adding null values to arraylist How to iterate through an ArrayList of Objects of ArrayList of Objects? Dynamically adding elements to ArrayList in Groovy How to replace existing value of ArrayList element in Java How to remove the last element added into the List? How to append elements at the end of ArrayList in Java? Removing Duplicate Values from ArrayList How to declare an ArrayList with values? In Java, can you modify a List while iterating through it? Load arrayList data into JTable