[英]Java How to create a ArrayList with multiple columns/datatypes
我试图创建一个ArrayList,它将具有3列类型(日期,日期,字符串)
因此,它实际上就像多个值的表,例如:
Jan 11, Jan 11, "started"
Jan 11, Jan 15, "running"
...
Jan 20, Jan 23, "slowing"
Jan 23, Jan 23, "stopped"
我将如何创建这样的ArrayList? 还是有另一种更好的数据格式? 抱歉,我是Java的新手,正在尝试学习。 另外,我需要能够在不同的类之间传递此数据结构。
非常感谢!
创建一个持有人类别:
class Holder{
//your fields : Date, Date, String
}
然后:
ArrayList<Holder> list = new ArrayList<Holder>();
创建一个具有您的属性的类,然后用您的类类型声明一个列表,例如:
public class MyClass {
private Date d1;
private Date d2;
private String s;
}
然后将list声明为:
ArrayList<MyClass> list = new ArrayList<MyClass>();
public class MyObject() {
Date date1;
Date date2;
String status;
public MyObject() {
}
//Getters & Setters
}
然后在主要或您要引用此数据的地方
List< MyObject> list = new ArrayList<>();
Java没有可容纳不同类型的数据结构。 但是,我创建了适合您的自己的元组数据结构。
样品用法:
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];
}
}
public class MyObject {
Date d1;
Date d2;
String s;
}
List<MyObject> list = new ArrayList<MyObject>();
可以使用HashMap是最好的方法
如何使用HashMap,(此示例为@Stephen C)
The value in the HashMap, to find values are sought by means of the associated key.
The Hashmap provide quick search
声明实例
//1
HashMap<String, Integer> hashMap = new HashMap<String, Integer>();
//2
HashMap<String, ArrayList<String>> hashMap2= new HashMap<String, ArrayList<String>>()
放
//1
hashMap.put("Value1", new Integer(1));
//2
ArrayList<String> arrList = new ArrayList<String>();
arrList .add("value");
hashMap2.put("value1", arrList );
得到
//1
hashMap.containsValue(1));
//2
while(hashMap2.hasNext()) {
System.out.println("Key = '" + key + "' has values: " + values);
}
我希望这些示例可以为许多不理解为“ Stephen C”的人提供解决方案。
正如其他人已经解释的那样,在Java中这样做的惯用方式是为三元组创建一个类。
我想添加其他内容,那就是对您的实际数据进行建模。 不用称它们为d1,d2和s,而是将字段实际命名为它们。
我不知道您的日期代表什么,但这是我的意思的一个示例:
class Record {
Date begin, end;
String status;
}
List<Record> records;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.