在Java中获取ArrayList<ArrayList<String>>的值(字符串)。

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个元素。


你在调用myList或String s时得到了null? - drew moore
6
如果你得到了null,那意味着集合中第一个列表的第二个元素是null。我们可以如何帮助? - JB Nizet
我要更新我的问题。 - boski
5个回答

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

在这里,您正在清空列表并添加一个元素(“第一个”),listOfSomething的第一个引用也会被更新,因为两个引用引用了同一个对象,所以当您访问不存在的第二个元素myList.get(1)时,您会得到null。

请注意,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);

是的,正确的做法是使用listOfZ = new ArrayList<String>();而不是listOfZ.clear();。 - boski

11

因为在清空列表之后,第二个元素为空

使用:

String s = myList.get(0);

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


是的,那是真的,但我写下示例代码时犯了一个愚蠢的错误。 - boski

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

5
更简洁的迭代列表的方法是:
// initialise the collection
collection = new ArrayList<ArrayList<String>>();
// iterate
for (ArrayList<String> innerList : collection) {
    for (String string : innerList) {
        // do stuff with string
    }
}

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
        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接