java 如何在java中定义一个包含两列的arrayList?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3482953/
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
how to define an arrayList with two columns in java?
提问by Red Lion
I have a hashMap. Each "Value"is going to be a a list which will be mapped later on with my "Key"s. List is desired to look like this:
我有一个 hashMap。每个“值”将是一个列表,稍后将与我的“键”映射。列表需要看起来像这样:
[length,time][length,time][length,time]
[长度,时间][长度,时间][长度,时间]
For example: Key{srcAddr=x, dstAddr=y, srcPort=12345, dstPort=80} value{(6523,0.001),(124,0.05), () , (), ...}
例如: Key{srcAddr=x, dstAddr=y, srcPort=12345, dstPort=80} value{(6523,0.001),(124,0.05), () , (), ...}
I just wonder how can I have a two-col arrayList.
我只是想知道我怎么能有一个两列的 arrayList。
package myclassifier;
import java.util.ArrayList;
public class FlowStatics {
int packetLength;
double timeArrival;
public FlowStatics(int pLength, double tArrival)
{
this.packetLength = pLength;
this.timeArrival = tArrival;
}
} and here is how I used it:
这是我如何使用它:
final ArrayList<FlowStatics> staticsArray = new ArrayList<FlowStatics>();
final HashMap<Flows, ArrayList> myHashMap = new HashMap<Flows, ArrayList>();
FlowStatics flowStatics = new FlowStatics(packetLength,timeArrival);
staticsArray.add(flowStatics);
myHashMap.put(flows, staticsArray);
and here is the part that I am reading it:
这是我正在阅读的部分:
Iterator<Flows> iterator = myHashMap.keySet().iterator();
while(iterator.hasNext()){
Flows key = iterator.next();
ArrayList value = myHashMap.get(key);
System.out.println("Fows"+key+"----------"+"Statics"+ value);
回答by Bozho
Well, your FlowStaticsis the correct solution
嗯,你FlowStatics是正确的解决方案
List<FlowStatics>will give you the "two-column array list".
List<FlowStatics>会给你“两列数组列表”。
Update: as of your update, myHashMap.put(flows, flowStatics);is wrong. You are this putting an individual pair, rather than a list in the map. You should use:
更新:截至您的更新,myHashMap.put(flows, flowStatics);是错误的。您是将一对单独的对,而不是地图中的列表。你应该使用:
staticsArray.add(flowStatics);
myHashMap.put(flows, staticsArray);
回答by polygenelubricants
A List<E>is an abstraction for a homogeneous list of elements whose type is E. There are some restrictions (e.g. no primitives), but conceptually the type Ecan be defined to be whatever you want.
AList<E>是类型为 的同构元素列表的抽象E。有一些限制(例如没有原语),但从概念上讲,E可以将类型定义为您想要的任何类型。
Suppose there's an abstraction of Pair<L,R>. Then a List<Pair<L,R>>is still a list of some E, but now that Eis a Pair<L,R>. So it's still a "one-column" list, but each element in the list is a "pair", so it's sort of a "two-column" list.
假设有一个 的抽象Pair<L,R>。然后 aList<Pair<L,R>>仍然是 some 的列表E,但现在E是 a Pair<L,R>。所以它仍然是一个“一列”列表,但列表中的每个元素都是一个“对”,所以它是一个“两列”列表。
Note that you don't always need a generic Pair<L,R>. Any type Ethat properly encapsulates all the information can be used in a List<E>.
请注意,您并不总是需要通用的Pair<L,R>. 任何E正确封装所有信息的类型都可以在List<E>.
And by the way, you can have a List<List<E>>too.
顺便说一句,你也可以拥有一个List<List<E>>。
See also
也可以看看
Related questions
相关问题
Returning values analogy
返回值类比
Often people ask "How can I return two values in Java?". The answer is analogous. You return one value, a new type which encapsulates both information.
经常有人问“我如何在 Java 中返回两个值?”。答案是类似的。您返回一个值,一种封装了这两种信息的新类型。
So instead of:
所以而不是:
// attempt to return two values
// DOES NOT COMPILE
return "James Bond";
return "007";
You do:
你做:
return new SecretAgent("James Bond", "007");

