java 如何实现嵌套的 ArrayList?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4126272/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 04:53:32  来源:igfitidea点击:

How do I implement nested ArrayList?

javaarraysmultidimensional-arrayarraylist

提问by js0823

I want to implement a data structure which looks something like this.

我想实现一个看起来像这样的数据结构。

{{RowID, N1, N2, N3},
 {RowID, N4, N5, N6},
 {RowID, N7, N8, N9}}

And goes on. It basically is a table in Java with 3 columns and RowID. What data structure should I use and how do I implement it as in code?

然后继续。它基本上是一个带有 3 列和 RowID 的 Java 表。我应该使用什么数据结构以及如何在代码中实现它?

采纳答案by Scott

Assuming that RowID is a long and the column data are Doubles, I would implement this construct as:

假设 RowID 是一个 long 并且列数据是 Doubles,我会实现这个结构:

import java.util.HashMap;
import java.util.Map;
...
Map<Long, Double[]> table = new HashMap<Long, Double[]>();

To store a row:

存储一行:

Long rowID = 1234L;
table.put(rowID, new Double {0.1, 0.2, 0.3});

To access a row:

要访问一行:

Double[] row = table.get(rowID);

Replace Double[] with whatever data type you desire Int[], String[], Object[] ...

将 Double[] 替换为您想要的任何数据类型 Int[], String[], Object[] ...

You may loop through this data with an iterator:

您可以使用迭代器遍历此数据:

import java.util.Iterator;
import java.util.Map.Entry;
...
Iterator<Entry<Long, Double[]>> iter = table.entrySet().iterator();
while (iter.hasNext()) {
    Entry entry = iter.next();
    rowID = entry.getKey();
    row = entry.getValue();
};

To iterate the data in the order data was inserted, use LinkedHashMap in place of HashMap.

要按照插入数据的顺序迭代数据,请使用 LinkedHashMap 代替 HashMap。

回答by Rafe Kettler

Make an ArrayList of ArrayLists. E.g.:

制作一个由 ArrayLists 组成的 ArrayList。例如:

ArrayList<ArrayList> arrayListOfLists = new ArrayList<ArrayList>();
arrayListOfLists.add(anotherArrayList);
//etc...

回答by Vincent Ramdhanie

There are several options. One way is to declare a class that represents a row.

有几种选择。一种方法是声明一个表示行的类。

 public class MyRow{
     private long rowId;
     private int col1;
     private int col2;
     private int col3;
     //etc
 }

Obviously you choose appropriate data types and variable names.

显然,您选择了适当的数据类型和变量名称。

Then you can create an ArrayList of this type:

然后你可以创建一个这种类型的 ArrayList:

   List<MyRow> rows = new ArrayList<MyRow>();

This is especially useful if the number of columns will not vary.

如果列数不变,这尤其有用。

回答by mR_fr0g

You could use a Map<Integer, ArrayList<MyObject>>where the key to the map would be your RowID.

您可以使用Map<Integer, ArrayList<MyObject>>地图的键是您的 RowID。

回答by Scott

I would create a bean object that contains the data for each row. That has an advantage over a "nested ArrayList" because the data members are strongly-typed.

我将创建一个包含每一行数据的 bean 对象。这比“嵌套 ArrayList”有优势,因为数据成员是强类型的。

Next, I would insert these beans into a List, probably a LinkedList unless you know the number of them ahead of time. If so, I would switch to an ArrayList.

接下来,我会将这些 bean 插入到一个 List 中,可能是一个 LinkedList,除非您提前知道它们的数量。如果是这样,我会切换到 ArrayList。

If order is not important, you could use a HashSet or HashMap instead, depending on if you are only iterating them (Set) or need to do key lookups by RowID (Map). If you use one of these data structures, you will need to override equals()and hashCode()for your bean.

如果顺序不重要,则可以改用 HashSet 或 HashMap,具体取决于您是仅迭代它们 (Set) 还是需要按 RowID (Map) 进行键查找。如果您使用这些数据结构之一,您将需要为您的 bean覆盖equals()hashCode()

回答by kiedysktos

Java provides list casting, so for example you can do this in following way:

Java 提供列表转换,因此例如您可以通过以下方式执行此操作:

ArrayList<List<someObject>> ArrayListOfLists = new ArrayList<List<someObject>>();