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

查看:173
本文介绍了我如何实现嵌套的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}}

和不胜枚举。它基本上是在Java中一个表3列与rowid。
我应该使用什么数据结构,以及如何实现它在code?

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是一个漫长而列数据是双打,我会实现这个构造为:

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

替换双[]与任何数据类型,你的愿望INT []的String [],[对象] ...

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

要重复的顺序数据数据被插入,在地方的HashMap的使用LinkedHashMap的。

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

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

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