如何实现嵌套的 ArrayList? [英] How do I implement nested ArrayList?

查看:52
本文介绍了如何实现嵌套的 ArrayList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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?

推荐答案

假设 RowID 为 long 且列数据为 Doubles,我将实现此构造为:

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

用你想要的任何数据类型替换 Double[] Int[], String[], Object[] ...

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

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

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

这篇关于如何实现嵌套的 ArrayList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆