2013-04-11 175 views
9

我知道這是很簡單的問題,但在獲取ArrayList的值(String)<ArrayList <String>>();在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); 

我想借此從字符串的ArrayList ArrayList中,我不知道該怎麼做。例如我去

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

它的工作原理!但是:

大更新:

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

回到我java.lang.IndexOutOfBoundsException:無效指數1,大小爲0 當我點擊這確實應該存在的項目。我沒有顯示生成ListView的代碼,但我可以告訴你,我的listOfS包含3個項目: 首先是空listOfZ,第二個listOfZ有2個元素,第三個listOfZ有1個元素。

+0

你得到空上通話 - myList中或成String? – drewmoore 2013-04-11 08:59:56

+6

如果您得到空值,則表示集合中第一個列表的第二個元素爲空。我們怎麼幫忙? – 2013-04-11 09:00:10

+0

那麼我會更新我的問題。 – boski 2013-04-11 09:02:28

回答

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

您在這裏(「第一」)清除列表和添加一個元素,的listOfSomething第一參考更新爲well sonce都引用相同的對象,所以當你訪問第二個元素myList.get(1)(它不再存在)時,你會得到null。

通知collection.Add(listOfSomething);保存兩個引用到同一個數組列表對象。

你需要爲兩個元素創建兩個不同的實例:

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

是的。正確。我使用listOfZ = new ArrayList ();而不是listOfZ.clear(); – boski 2013-04-11 09:44:06

4

迭代裏面名單列表上正確的做法是:

//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

由於第二個元素是空您清除列表之後。

用途:

String s = myList.get(0); 

記住,指數0是第一要素

+0

是的,這是真的,但我寫下示例代碼,我犯了一個愚蠢的錯誤。 – boski 2013-04-11 09:43:18

4

迭代名單的清潔方法是:

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