我知道这是一个简单的问题,但是在
中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:索引1无效,大小为0 当我点击真正存在的项目时。我没有显示生成ListView的代码,但我可以告诉你我的listOfS包含3个项目: 首先是Null listOfZ,第二个listOfZ得到2个元素,第三个listOfZ得到1个元素。
答案 0 :(得分:12)
listOfSomething.Clear();
listOfSomething.Add("first");
collection.Add(listOfSomething);
您正在清除此处的列表并添加一个元素(“first”),listOfSomething
的第一个引用也会更新,而sonce都引用相同的对象,因此当您访问第二个元素{{1}时}(它不再存在)你得到null。
注意两个myList.get(1)
保存对同一个arraylist对象的两个引用。
您需要为两个元素创建两个不同的实例:
collection.Add(listOfSomething);
答案 1 :(得分:11)
因为清除列表后第二个元素为空。
使用:
String s = myList.get(0);
请记住,索引0是第一个元素。
答案 2 :(得分: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);
}
}
答案 3 :(得分:4)
迭代列表的一种更简洁的方法是:
// initialise the collection
collection = new ArrayList<ArrayList<String>>();
// iterate
for (ArrayList<String> innerList : collection) {
for (String string : innerList) {
// do stuff with string
}
}
答案 4 :(得分: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
}