Java 如何创建具有多个列/数据类型的 ArrayList
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22303352/
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
Java How to create a ArrayList with multiple columns/datatypes
提问by A Y
I am trying to create an ArrayList which will have 3 columns of type (Date, Date, String)
我正在尝试创建一个 ArrayList,它将有 3 列类型(日期、日期、字符串)
So it is really like table of multiple values, for example:
所以它真的很像多值表,例如:
Jan 11, Jan 11, "started"
Jan 11, Jan 15, "running"
...
Jan 20, Jan 23, "slowing"
Jan 23, Jan 23, "stopped"
How would I go about creating an ArrayList like this? Or is there another data format that is better? Sorry I am very new to Java and trying to learn. Also, I need to be able to pass this data structure between different classes.
我将如何创建这样的 ArrayList?或者有其他更好的数据格式吗?对不起,我对 Java 很陌生,正在努力学习。另外,我需要能够在不同的类之间传递这个数据结构。
Thanks a lot!
非常感谢!
回答by Salah
Create a class the have your properties then declare a list with your class type like:
创建一个具有您的属性的类,然后使用您的类类型声明一个列表,例如:
public class MyClass {
private Date d1;
private Date d2;
private String s;
}
Then declare list as:
然后将列表声明为:
ArrayList<MyClass> list = new ArrayList<MyClass>();
回答by Eugene
Create a holder class:
创建一个持有者类:
class Holder{
//your fields : Date, Date, String
}
Then:
然后:
ArrayList<Holder> list = new ArrayList<Holder>();
回答by ruhungry
public class MyObject {
Date d1;
Date d2;
String s;
}
List<MyObject> list = new ArrayList<MyObject>();
回答by j.con
public class MyObject() {
Date date1;
Date date2;
String status;
public MyObject() {
}
//Getters & Setters
}
and then in main, or where you are referencing this data
然后在 main 中,或者在您引用这些数据的地方
List< MyObject> list = new ArrayList<>();
回答by cat_minhv0
Java does not have a data structure to hold different types. However, I create my own Tupledata structure that would be appropriate for you.
Java 没有保存不同类型的数据结构。但是,我创建了适合您的自己的元组数据结构。
Sample usage :
示例用法:
public class TupleTest {
@Test
public void testOneType() {
Tuple t1 = Tuple.Factory.create(1);
Tuple t2 = Tuple.Factory.create(1);
assertEquals(t1, t2);
}
@Test
public void testTwoTypes() {
Tuple t1 = Tuple.Factory.create(1, "a");
Tuple t2 = Tuple.Factory.create(1, "a");
assertTrue(t1.equals(t2));
}
@Test
public void testGetIndex() {
Tuple t1 = Tuple.Factory.create(1, "a");
assertTrue((Integer) t1.get(0) == 1);
}
}
public class Tuple {
private final Object[] items;
private Tuple(Object... items) {
this.items = items;
}
public static class Factory {
public static Tuple create(final Object... items) {
return new Tuple(items);
}
}
/**
*
* @return
*/
public synchronized int size() {
return items.length;
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
int hash = 17;
for (Object obj : items) {
if (obj != null)
hash = hash * 31 + obj.hashCode();
}
return hash;
}
@Override
public boolean equals(Object obj) {
// TODO Auto-generated method stub
boolean res = true;
final int size = this.size();
final Tuple p = (Tuple) obj;
if (!(obj instanceof Tuple))
res = false;
if (obj == null || this == obj)
res = false;
if (p.size() != size)
res = false;
for (int i = 0; i < size; i++) {
if (!p.items[i].getClass().equals(this.items[i].getClass()))
res = false;
if (!p.items[i].equals(this.items[i]))
res = false;
}
return res;
}
/**
* Return string of multiple types "String.class Number.Class Integer.Class"
*/
@Override
public String toString() {
// TODO Auto-generated method stub
StringBuilder sb = new StringBuilder();
for (Object item : items) {
sb.append(item.toString() + " ");
}
return sb.toString();
}
public Object get(int index) {
return items[index];
}
}
回答by rsudha
In ArrayListyou can't have multiple columns, ArrayList
is similar to one dimensional array and you can store objects of any datatype.
在ArrayList 中,您不能有多个列,ArrayList
类似于一维数组,您可以存储任何数据类型的对象。
The solution depends on the problem you are trying to resolve.
If the data you are trying to store in one row are related and have some unique identity you can use HashMap,
or you can create a class with the columns as fields and store the instances of that class in the ArrayList
.
解决方案取决于您要解决的问题。如果您尝试在一行中存储的数据相关并且具有某些唯一标识,您可以使用HashMap,或者您可以创建一个以列作为字段的类并将该类的实例存储在ArrayList
.
Hope this helps.
希望这可以帮助。
回答by delive
You can use HashMap is the best method
您可以使用 HashMap 是最好的方法
How to use HashMap, (this example is to @Stephen C)
如何使用HashMap,(这个例子是@Stephen C)
The value in the HashMap, to find values are sought by means of the associated key.
The Hashmap provide quick search
Examples of declaration
声明示例
//1
HashMap<String, Integer> hashMap = new HashMap<String, Integer>();
//2
HashMap<String, ArrayList<String>> hashMap2= new HashMap<String, ArrayList<String>>()
put
放
//1
hashMap.put("Value1", new Integer(1));
//2
ArrayList<String> arrList = new ArrayList<String>();
arrList .add("value");
hashMap2.put("value1", arrList );
get
得到
//1
hashMap.containsValue(1));
//2
while(hashMap2.hasNext()) {
System.out.println("Key = '" + key + "' has values: " + values);
}
I hope these examples can provide a solution to many people who did not understand as "Stephen C".
我希望这些例子可以为许多不理解为“Stephen C”的人提供解决方案。
回答by folkol
As others have already explained, the idiomatic way of doing this in Java is to create a class for your triple.
正如其他人已经解释的那样,在 Java 中执行此操作的惯用方法是为您的三元组创建一个类。
I would like to add something else though, and that is to model your actual data. Instead of calling them d1, d2 and s - name the fields for what they actually are.
不过,我想添加一些其他内容,那就是对您的实际数据进行建模。与其将它们称为 d1、d2 和 s,不如将它们命名为它们的实际名称。
I don't know what your dates represent, but this is an example of what I mean:
我不知道你的日期代表什么,但这是我的意思的一个例子:
class Record {
Date begin, end;
String status;
}
List<Record> records;