Java 列表列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3102761/
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
List of Lists of Lists
提问by
I'm new to Java and I need to make a list of lists of lists. I could do it in python because an element of a list can be a list so in an embedded list list[0]
would refer to a list and list[0][0]
would refer to the zeroeth element of the embedded list. Is there any easy way to implement this behavior in java?
我是 Java 新手,我需要制作一个列表列表。我可以在 python 中做到这一点,因为列表的元素可以是列表,因此在嵌入列表list[0]
中将引用列表并list[0][0]
引用嵌入列表的第零个元素。有没有什么简单的方法可以在java中实现这种行为?
采纳答案by Mario Fusco
All the other answers are technically correct, but IMHO if you implement a rough List of Lists of Lists you are not treating your data at the right level of abstraction. For example I am pretty sure that a List of Lists already means "something" in your business domain. Encapsulate this "something" in another object so you can just have a List<Something> instead of a difficult to use and maintain List<List<List<Object>>>.
所有其他答案在技术上都是正确的,但恕我直言,如果您实现了一个粗略的列表列表,您就没有在正确的抽象级别处理您的数据。例如,我很确定 List of Lists 在您的业务领域中已经意味着“某物”。将这个“东西”封装在另一个对象中,这样你就可以拥有一个 List<Something> 而不是一个难以使用和维护的 List<List<List<Object>>>。
回答by jjnguy
As Mariosays, you probably need to abstract out your data a little further. But, the following will do what you need.
正如马里奥所说,您可能需要进一步抽象出您的数据。但是,以下将满足您的需求。
In Java you would so something like:
在 Java 中,你会像这样:
List<List<List<Object>>> listOfListsOfLists =new ArrayList<List<List<Object>>>();
Then to access the items, you would use:
然后要访问这些项目,您将使用:
listOfListsOfLists.get(a).get(b).get(c);
Or, to iterate over everything:
或者,迭代所有内容:
for (List<List<Object>> list2: listOfListsOfLists) {
for (List<Object> list1: list2) {
for (Object o: list1) {
// use `o`
}
}
}
回答by Mark Peters
Since all of these answers make me barf, can I just add the suggestion that you either
由于所有这些答案都让我感到恶心,我可以添加以下建议吗?
Create a data type to express your data while encapsulating the details of the structure, or at least
Create a key type that wraps an int[] (but overrides equals and hashCode properly) and use a HashMap instead? It's typically rare that your whole 3-dimensional structure will be filled up much anyway.
创建一个数据类型来表达你的数据,同时封装结构的细节,或者至少
创建一个包装 int[] 的键类型(但正确覆盖 equals 和 hashCode)并使用 HashMap 代替?无论如何,您的整个 3 维结构通常很少会被填满。
Even better you could encapsulate that map and use varargs for clean access.
更好的是,您可以封装该映射并使用可变参数进行干净的访问。
public class NDimensionalArray<V> {
private final int dimensions;
private final Map<Key, V> values = new HashMap<Key, V>();
private NDimensionalArray(int dimensions) {
this.dimensions = dimensions;
}
public V get(int... indices) {
checkIndices(indices);
return values.get(new Key(indices));
}
public void set(V value, int... indices) {
checkIndices(indices);
values.put(new Key(indices), value);
}
private void checkIndices(int[] indices) {
if ( indices.length != dimensions ) {
throw new IllegalArgumentException();
}
}
private static final class Key {
private final int[] indices;
private Key(int[] indices) {
this.indices = indices;
}
@Override
public int hashCode() {
return Arrays.hashCode(indices);
}
@Override
public boolean equals(Object obj) {
return Arrays.equals(indices, ((Key)obj).indices);
}
}
}
If people have examples of established collections libraries that already do this sort of thing, let me know and I'll add links.
如果人们有已经建立的收藏库的例子,它们已经在做这种事情,请告诉我,我会添加链接。
回答by Andreas Dolk
A comprehensive example showing List-of-List with collections and generics (Java 1.5+)
显示带有集合和泛型的 List-of-List 的综合示例(Java 1.5+)
// declare the list of lists
List<List<String>> listOfListOfStrings = new ArrayList<List<String>>();
// populate
List<String> listOfStrings = new ArrayList<String>(); // one inner list
listOfStrings.add("one-one");
listOfStrings.add("one-two");
listOfListOfStrings.add(listOfStrings);
listOfStrings = new ArrayList<String>(); // and another one
listOfStrings.add("two-one");
listOfStrings.add("two-two");
listOfListOfStrings.add(listOfStrings);
// access
String oneOne = listOfListOfStrings.get(0).get(0); // first element of first inner list
String twoTwo = listOfListOfStrings.get(1).get(1); // second element of second inner list
回答by Jay
While it is certainly true that you can construct a List<List<List<whatever>>> in Java, I can't help but wonder, Why do you want to do this? Not that it's inconceivable that this is the best solution to your problem, but wow, like why?
虽然您可以在 Java 中构造 List<List<List<whatever>>> 确实是正确的,但我不禁想知道,您为什么要这样做?并不是说这是您问题的最佳解决方案是不可思议的,但是哇,为什么?
I guess I could imagine something like
我想我可以想象出类似的东西
public class Employee ...
List<Employee> store; // all the employees in a store
List<List<Employee>> city; // all the store lists for a city
List<List<List<Employee>>> nation; // all the store lists for the nation
But would you really want to process it that way? I don't know, it depends on what you need to do with it.
但你真的想这样处理吗?我不知道,这取决于你需要用它做什么。