Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 200
boski Avatar asked Apr 11 '13 08:04

boski


People also ask

How to get ArrayList of strings in Java?

Input : ArrayList = [ "Jupiter", "Saturn", "Uranus", "Neptune", "Sun"] Output: String [] = {"Jupiter", "Saturn", "Uranus", "Neptune", "Sun"} Get the ArrayList of Strings. Find the size of ArrayList using size () method, and Create a String Array of this size. Fetch each element of the ArrayList one by one using get () method.

How do I get the size of an ArrayList in Java?

Get the ArrayList of Strings. Find the size of ArrayList using size() method, and Create a String Array of this size. Fetch each element of the ArrayList one by one using get() method. Assigned each element into String Array using assignment(=) operator.

What is ArrayList get (index) method in Java?

ArrayList get(index) method in Java with examples. The get() method of ArrayList in Java is used to get the element of a specified index within the list. Syntax : Parameter : index:index of the elements to be returned. It is of data-type int. Returns : It returns the element at the specified index in the given list.

What is the difference between ArrayList and array in Java?

Java ArrayList The ArrayList class is a resizable array , which can be found in the java.util package. The difference between a built-in array and an ArrayList in Java, is that the size of an array cannot be modified (if you want to add or remove elements to/from an array, you have to create a new one).


1 Answers

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
    }
}
like image 50
user1897983 Avatar answered Sep 23 '22 16:09

user1897983