10

我知道这是一个简单的问题,但在

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 中取 String,我不知道该怎么做。例如我去

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: Invalid index 1, size is 0 当我点击真正应该存在的项目时。我没有显示生成 ListView 的代码,但我可以告诉你我的 listOfS 包含 3 个项目:第一个是 Null listOfZ,第二个 listOfZ 有 2 个元素,第三个 listOfZ 有 1 个元素。

4

5 回答 5

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

您在此处清除列表并添加一个元素(“第一个”),第一个引用listOfSomething也会更新,因为两者都引用同一个对象,因此当您访问第二个元素myList.get(1)(不再存在)时,您会得到空值。

请注意,两者都collection.Add(listOfSomething);保存了对同一个 arraylist 对象的两个引用。

您需要为两个元素创建两个不同的实例:

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);
于 2013-04-11T09:06:34.023 回答
11

因为清除列表后第二个元素为空。

利用:

String s = myList.get(0);

请记住,索引 0 是第一个元素

于 2013-04-11T09:06:42.583 回答
5

一种更简洁的迭代列表的方法是:

// initialise the collection
collection = new ArrayList<ArrayList<String>>();
// iterate
for (ArrayList<String> innerList : collection) {
    for (String string : innerList) {
        // do stuff with string
    }
}
于 2013-04-11T09:12:04.270 回答
5

迭代列表内列表的正确方法是:

//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);
    }
}
于 2013-04-11T09:06:30.623 回答
0
I have String array like this
We have to pass data through response.body.getdata and this data pass in constructor like this,

List taginnerData;

 "data": [
                "banana",
                "apple",
                "grapes",
                "Pears",
                "Mango",
                "Cherry",
                "Guava",
                "TorontoVsMilwaukee_12Jan19"
            ]

 String[] myArray = new String[taginnerData.size()];
        for (int i = 0; i < taginnerData.size(); i++) {
            myArray[i] = String.valueOf(taginnerData.get(i));
            holder.tv_channel_name.setText("" +taginnerData.get(i));
//we get any value from here to set in adapter
        }
于 2021-08-03T11:48:08.057 回答