2013-04-11 11 views
9

So che è semplice domanda, ma inOttieni valore (stringa) di ArrayList <ArrayList <String>>(); in Java

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

voglio prendere String da ArrayList di ArrayList, e non so come fare. Per esempio vado

ArrayList<String> myList = collection.get(0); 
String s = myList.get(0); 

e funziona! ma:

Big aggiornamento:

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; 
    } 
me

ritorno java.lang.IndexOutOfBoundsException: Indice non valido 1, la dimensione è 0 quando clicco sulla voce che in realtà dovrebbe esistere. Non ho mostrato il codice che genera ListView, ma posso dirti che il mio listOfS contiene 3 elementi: prima è Null listOfZ, il secondo listOfZ ha 2 elementi, il terzo listOfZ ha 1 elemento.

+0

Si ottiene nulla su cui chiamare - a myList o String s? – drewmoore

+6

Se hai null, vuol dire che il secondo elemento del primo elenco nella raccolta è nullo. Come potremmo aiutare? –

+0

Bene, ho intenzione di aggiornare la mia domanda. – boski

risposta

12
listOfSomething.Clear(); 
listOfSomething.Add("first"); 
collection.Add(listOfSomething); 

si sta cancellando la lista qui e l'aggiunta di un elemento ("prima"), il 1 ° di riferimento di listOfSomething viene aggiornato beh sonce entrambi fanno riferimento allo stesso oggetto, quindi quando si accede al secondo elemento myList.get(1) (che non esiste più) si ottiene il null.

Avviso entrambi collection.Add(listOfSomething); salvano due riferimenti allo stesso oggetto arraylist.

È necessario creare due istanze diverse per due elementi:

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

Sì. Corretta. Io uso listOfZ = new ArrayList (); invece di listOfZ.clear(); – boski

4

Il modo giusto per iterare su una lista lista interna è:

//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); 
    } 
} 
11

Poiché il secondo elemento è nullo dopo si cancella la lista.

Usa:

String s = myList.get(0); 

E ricordate, indice 0 è il primo elemento.

+0

Sì, era vero, ma scrivo codice di esempio e faccio uno stupido errore. – boski

4

un modo più pulito di iterazione delle liste è:

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