如何实现嵌套ArrayList?

发布于 2024-09-30 19:51:05 字数 185 浏览 1 评论 0 原文

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

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

儭儭莪哋寶赑 2024-10-07 19:51:05

创建一个 ArrayList 的 ArrayList。例如:

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

Make an ArrayList of ArrayLists. E.g.:

ArrayList<ArrayList> arrayListOfLists = new ArrayList<ArrayList>();
arrayListOfLists.add(anotherArrayList);
//etc...
离不开的别离 2024-10-07 19:51:05

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

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

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

然后您可以创建此类型的 ArrayList:

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

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

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:

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

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

柏拉图鍀咏恒 2024-10-07 19:51:05

假设 RowID 是 long 并且列数据是 Doubles,我会将此构造实现为:
<代码>

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

存储一行:
<代码>

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

访问一行:
<代码>

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

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

您可以使用迭代器循环访问这些数据:
<代码>

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();
};

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

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

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[] ...

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.

陈年往事 2024-10-07 19:51:05

您可以使用 Map> ,其中映射的键是您的 RowID。

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

陪你到最终 2024-10-07 19:51:05

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

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

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

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.

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.

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.

清欢 2024-10-07 19:51:05

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

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

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

ArrayList<List<someObject>> ArrayListOfLists = new ArrayList<List<someObject>>();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文