Java 如何在ArrayList的Arraylist中添加和检索值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24755235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add and retrieve the values in Arraylist of ArrayList
提问by manpreetSingh
I know how to createan ArrayList
of ArrayList
, but how to addnew ArrayList
and addvalue to that particular ArrayList
and how to retrievethe data from that list.
我知道如何创建一个ArrayList
的ArrayList
,但如何添加新的ArrayList
和增加值,以特定的ArrayList
,以及如何检索从列表中的数据。
ArrayList<ArrayList<Integer>> arrayList = new ArrayList<ArrayList<Integer>>();
采纳答案by CoderCroc
how to add new ArrayLists and ADD value to that particular ArrayList and how to RETRIEVE the data form that list.
如何向该特定 ArrayList 添加新的 ArrayLists 和 ADD 值,以及如何从该列表中检索数据。
ArrayList<ArrayList<Integer>> arrayList=new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> newAL= new ArrayList<Integer>();
arrayList.add(newAL);
newAL.add(1);
newAL.add(2);
newAL.clear();
System.out.println(arrayList.get(0));
//Changes persist in your arraylist
So after adding ArrayList you can manipulate newAL
as ArrayList
stores referenceyou don't need to fetch and set element from main arrayList
.
因此,在添加 ArrayList 后,您可以将其newAL
作为ArrayList
存储引用进行操作,而无需从 main 获取和设置元素arrayList
。
To retrive data you can Iterate
(Use ForEach Loop) over arrayList
or you can do following
要检索数据,您可以Iterate
(使用ForEach 循环),arrayList
或者您可以执行以下操作
Integer List0Item1=arrayList.get(0).get(1);//Get first element of list at 0
arrayList.get(0).set(0, 10);//set 0th element of list at 0 as 10
ArrayList<Integer> list10=arrayList.get(10);//get arraylist at position 10
回答by Ruchira Gayan Ranaweera
You can try this
你可以试试这个
ArrayList<ArrayList<Integer>> arrayList = new ArrayList<>();
ArrayList<Integer> list1=new ArrayList<>();
ArrayList<Integer> list2=new ArrayList<>();
list1.add(1); // add to list
list1.add(2);
list2.add(3);
list2.add(4);
arrayList.add(list1); // add to list of list
arrayList.add(list2);
for(ArrayList<Integer> i:arrayList){ // iterate -list by list
for(Integer integer:i){ //iterate element by element in a list
}
}
You can get element directly
可以直接获取元素
arrayList.get(0).get(0); // 0th list 0th value
adding value to 0th list
将值添加到第 0 个列表
arrayList.get(0).add(1); // 1 will add to 0th index list-list1
回答by Bindumalini KK
It is a list of list where every object of the parent list is in turn a sublist.The code is something like below:
它是一个列表列表,其中父列表的每个对象又是一个子列表。代码如下所示:
List<List<String>> mainList = new ArrayList<List<String>>();
List<String> sublist1 = new ArrayList<String>();
sublist1.add("State");
sublist1.add("Country");
sublist1.add("City");
mainList .add(sublist1);
List<String> sublist2 = new ArrayList<String>();
sublist2.add("Sleep");
sublist2.add("Suspend");
sublist2.add("Wait");
mainList .add(sublist2);
for(List<String> obj:mainList){ // iterate
for(String value:obj){
System.out.println(value);
}
}
}
}
Hope this will help to clear your doubt.