2013-04-11 109 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 
    } 
} 
相关问题