Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
    
You get null on which call - to myList or to String s? –  drewmoore Apr 11 '13 at 8:59
6  
If you got null, it means the second element of the first list in the collection is null. How could we help? –  JB Nizet Apr 11 '13 at 9:00
    
Well I am going to update my question. –  boski Apr 11 '13 at 9:02

4 Answers 4

up vote 6 down vote accepted
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);
share|improve this answer
    
Yes. Correct. I use listOfZ = new ArrayList<String>(); instead of listOfZ.clear(); –  boski Apr 11 '13 at 9:44

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.

share|improve this answer
    
Yes that was true, but I write down example code and I make stupid mistake. –  boski Apr 11 '13 at 9:43

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);
    }
}
share|improve this answer

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
    }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.