Java 如何正确创建 ArrayList 的 ArrayList?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19758449/
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 correctly create an ArrayList of ArrayLists?
提问by java
I am trying to create an ArrayList of ArrayLists. I want to create 25 ArrayLists and have those ArrayList hold different values. The goal being to create a sorted dictionary by word length.
我正在尝试创建一个 ArrayLists 的 ArrayList。我想创建 25 个 ArrayLists 并让这些 ArrayList 保存不同的值。目标是创建一个按字长排序的字典。
My code looks like
我的代码看起来像
for(int i = 0; i < 25; i++)
list2D.add(list);
for(int i = 0; i < stringList; i++)
list2D.get(stringList.get(i).length()).add(stringList.get(i))
The problem is that every list contains the same values after it finishes.
问题是每个列表在完成后都包含相同的值。
I know why the problem is happening. "list" is an ArrayList, ArrayList's are objects and if you edit an object then everything containing that object will be edited.
我知道为什么会出现问题。“list”是一个 ArrayList,ArrayList 是对象,如果您编辑一个对象,那么包含该对象的所有内容都将被编辑。
To fix the problem I tried
为了解决我尝试过的问题
for(int i = 0; i < 25; i++){
list = new ArrayList<String>();
for(int i2 = 0; i2 < stringList.size(); i2++){
if(stringList.get(i).length() == i)
list.add(stringList.get(i2));
}
list2D.add(list);
}
But when I test my "list2D"
但是当我测试我的“list2D”时
for(int i = 0; i < 25; i++)
System.out.print(list2D.get(i).size()+" ");
I get
我得到
0 0 0 0 0 58110 0 58110 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 58110 0 58110 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
and I have no idea why...
我不知道为什么...
Also it might be relevant to note that stringList contains 58110 values.
此外,可能需要注意 stringList 包含 58110 个值。
Also I don't want to have to make 25 different ArrayLists!
此外,我不想制作 25 个不同的 ArrayList!
采纳答案by Peter Lawrey
I would try the following to do a single pass of the stringList
我会尝试以下方法对 stringList 进行一次传递
List<List<String>> dict = new ArrayList<>();
for(string s: stringsList) {
int len = s.length();
// add new array lists as required, could be any length, assuming << 100
while(dict.size() <= len) dict.add(new ArrayList<String>());
// add the string to the right list.
dict.get(len).add(s);
}
回答by Display Name
This is easy. For example, you can create them like this: http://ideone.com/3NZwWU
这很简单。例如,您可以像这样创建它们:http: //ideone.com/3NZwWU
// just for convenience, initializes arraylist with values
static <T> ArrayList<T> AL(T... values) {
ArrayList<T> r = new ArrayList<T>();
for (T x : values) {
r.add(x);
}
return r;
}
public static void main (String[] args) throws java.lang.Exception
{
System.out.println(AL(
AL(3,2,24,131),
AL("this", "is", "second", "array")));
// prints `[[3, 2, 24, 131], [this, is, second, array]]`
}
回答by java
Thanks. I found a really easy solution though...
谢谢。不过我找到了一个非常简单的解决方案......
for(int i = 0; i < 25; i++)
list2D.add(new ArrayList<String>());
for(int i = 0; i < word.size(); i++)
list2D.get(stringList.get(i).length()).add(stringList.get(i));
回答by Ash Searle
//makes an arraylist with 19 arraylist elements
//创建一个包含 19 个 arraylist 元素的 arraylist
ArrayList<ArrayList> arrList = new ArrayList<ArrayList>();
for(int i = 1; i < 20; i++){
arrList.add(new ArrayList<Integer>(5*i+5));
}