bos*_*ski 10 java android arraylist
我知道这是一个简单的问题,但在
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);
Run Code Online (Sandbox Code Playgroud)
我想从ArrayList的ArrayList中获取String,我不知道该怎么做.比如我去
ArrayList<String> myList = collection.get(0);
String s = myList.get(0);
Run Code Online (Sandbox Code Playgroud)
它的工作原理!但:
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;
}
Run Code Online (Sandbox Code Playgroud)
return me java.lang.IndexOutOfBoundsException:索引1无效,当我点击真正存在的项目时,大小为0.我没有显示生成ListView的代码,但我可以告诉你我的listOfS包含3个项目:第一个是Null listOfZ,第二个listOfZ有2个元素,第三个listOfZ有1个元素.
Clo*_*ble 12
listOfSomething.Clear();
listOfSomething.Add("first");
collection.Add(listOfSomething);
Run Code Online (Sandbox Code Playgroud)
您正在清除此处的列表并添加一个元素("first"),第一个引用listOfSomething更新以及sonce都引用相同的对象,因此当您访问第二个元素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);
Run Code Online (Sandbox Code Playgroud)
Bob*_*der 11
因为清除列表后第二个元素为null.
使用:
String s = myList.get(0);
Run Code Online (Sandbox Code Playgroud)
请记住,索引0是第一个元素.
迭代列表中的列表的正确方法是:
//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);
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
迭代列表的一种更简洁的方法是:
// initialise the collection
collection = new ArrayList<ArrayList<String>>();
// iterate
for (ArrayList<String> innerList : collection) {
for (String string : innerList) {
// do stuff with string
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
127684 次 |
| 最近记录: |