[英]Java what data structure use for List with more different data types?
我想通过以下数据类型传递更多项目:
OneListItem = (Integer, String, Intent);
所以:
OneListItem.add(1,"Hello", IntentToPass);
OneListItem.add(2,"Hello", IntentToPass);
OneListItem.add(3,"Hello", IntentToPass);
并通过这种方式处理给定数据的调用方法:
nh.addNotification(OneListItem);
最佳的数据结构是什么(数据结构将始终是相同的),以及如何定义它?
我尝试过类似的方法,但是没有运气:
List<String, Integer, Intent> li = new ArrayList<String, Integer, Intent>;
感谢您的任何建议。
我将包装它并创建自己的类:
class MyClass {
private String str;
private Integer i1;
private Integer i2;
// ctrs, getters, setters
}
然后简单地:
List<MyCLass> li = new ArrayList<>();
实际上...您也可以通过这种方式进行操作,这与您要求的内容非常相似。
警告:我不建议使用它,我只是说有可能这样做。
Intent intent;
ArrayList<Object[]> objects = new ArrayList<Object[]>();
objects.add(new Object[]{1,"Hello", intent});
objects.add(new Object[]{2,"Hello", intent});
objects.add(new Object[]{3,"World", intent});
Log.d("OUTPUT", objects.get(2)[1]); // it prints out "World"
最简单的方法是为您要存储的任何类型的信息创建一个类。 例如:
class Person {
public final String name;
public final int age;
public final boolean gender;
public Person(String name, int age, boolean gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
}
然后将使用该列表,如下所示:
ArrayList<Person> people = new ArrayList<>();
people.add(person1);
people.add(person2);
people.add(person3);
也可以用声明为public
而不是final
成员变量来代替对“容器”类使用public final
成员变量,从而可以对其进行修改。 这完全取决于您是否希望类是不可变的,以及如何操作数据。
对于像这样的简单“容器”类,我反对使用getters / setter方法,因为它实际上只是不必要的编码。 只需使用public
非final
变量。 但是,访问类时,可能需要使用getter或setter来实现同步或其他处理。
您可以使用Apache Commons的MultiKeyMap,也可以定义自己的自定义类来保存所需的数据。
上课:
public class MyNotificationItem {
private final int first;
private final String second;
private final int third;
public MyNotificationItem(int first, String second, int third) {
this.first = first;
this.second = second;
this.third = third;
}
public int getFirst() {
return first;
}
public String getSecond() {
return second;
}
public int getThird() {
return third;
}
}
标准库中没有足够的表示形式。 最简单的方法是创建自己的Triple<A,B,C>
类,然后使用List<Triple<String,Integer,Intent>>
。
public final class Triple<A,B,C>{
public final A _1; //public final to avoid getters on example
public final B _2;
public final C _3;
public Triple(A a, B b, C c){
this._1 = a;
this._2 = b;
this._3 = c;
}
}
//usage would be like this
List<Triple<String, Integer, Intent>> li = new ArrayList<>;
Triple<String, Integer, Intent> t = new Triple(1,"Hello", IntentToPass);
li.add(t);
或者,您可以使用外部库,例如Apache Commons 的Triple
public class MyNotificationItem {
private final int first;
private final String second;
private final int third;
public MyNotificationItem(int first, String second, int third) {
this.first = first;
this.second = second;
this.third = third;
}
public int getFirst() {
return first;
}
public String getSecond() {
return second;
}
public int getThird() {
return third;
}
}
class OneListItem extends ArrayList<MyNotificationItem>{
public void add(Integer i, String j, Intent k)){
add(new MyNotificationItem(i, j, k));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.