在Java中的列表中存储不同类型的元素 [英] Storing different types of elements in a List in Java

查看:732
本文介绍了在Java中的列表中存储不同类型的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图开发一个通用表加载程序,其模式在运行时是已知的。这需要一个类,它包含不同类型的元素的列表,并支持各种get和set方法,例如 getInt(int index) asString int index) asStringList(int index)。我考虑的元素类型是 Integer Double String List List< Double> List< String& / code>。每个元素的实际类型在运行时是已知的,我将它们存储在列表中描述其模式以供进一步处理。



我的问题是: 列表< Object> 列表< extends Object> ?或者有更好的方法来实现这样的类吗?

解决方案

因为你的类的共同祖先 Object ,因为 List< ;? extends Object> 不会使事情变得更干净(毕竟,一切都扩展 Object )它看起来像 List< Object&



然而,这样的列表将是一个混合包:你需要检查对象的运行时类型内部,并基于此做出决策。这绝对不是一件好事。



一个更好的替代方法是创建自己的类,实现对列表元素的统一方法,并为每个子类创建一个子类子类型实现这些操作不同。

  public interface ItemWrapper { 
int calculateSomething();
}

public abstract class IntWrapper implements ItemWrapper {
private int value;

public IntWrapper(int v){
value = v;
}

public abstract int calculateSomething(){
return value;
}
}

public abstract class DoubleListWrapper implements ItemWrapper {
private List< Double>列表;

public DoubleListWrapper(List< Double> lst){
list = lst;
}

public int calculateSomething(){
int res;
for(Double d:list){
res + = d;
}

return res;
}
}
// ...等等

现在,您可以列出 ItemWrapper 对象和 calculateSomething 对象,而不检查它们的类型:

 列表< ItemWrapper> myList = new ArrayList< ItemWrapper>(); 

for(ItemWrapper w:myList){
System.out.println(
w.calculateSomething());
}


I'm trying to develop a general table loader which schema is known at runtime. This requires having a class which contains a list of different types of elements and supports various get and set method such as getInt(int index), asString(int index), asStringList(int index). The types of elements I consider are Integer, Double, String, and List<Integer>, List<Double> and List<String>. The actual type of each element is known in run time, and I will store them in a List describing its schema for further processing.

My question is: should I store such list of elements in List<Object> or List<? extends Object>? or is there better way to implement such class?

解决方案

Since the common ancestor of your classes is Object, and because List<? extends Object> does not make things any cleaner (after all, everything extends Object) it looks like List<Object> would be an OK choice.

However, such list would be a mixed bag: you would need to check run-time type of the object inside, and make decisions based on that. This is definitely not a good thing.

A better alternative would be creating your own class that implements operations on elements of the list the uniform way, and make one subclass for each subtype that implements these operations differently. This would let you treat the list in a uniform way, pushing the per-object differentiation into your wrappers.

public interface ItemWrapper {
    int calculateSomething();
}

public abstract class IntWrapper implements ItemWrapper {
    private int value;

    public IntWrapper(int v) {
      value=v; 
    }

    public abstract int calculateSomething() {
      return value;
    }
}

public abstract class DoubleListWrapper implements ItemWrapper {
    private List<Double> list;

    public DoubleListWrapper (List<Double> lst) {
      list = lst; 
    }

    public int calculateSomething() {
        int res;
        for (Double d : list) {
            res += d;
        }

        return res;
    }
}
// ...and so on

Now you can make a list of ItemWrapper objects, and calculateSomething on them without checking their type:

List<ItemWrapper> myList = new ArrayList<ItemWrapper>();

for (ItemWrapper w : myList) {
    System.out.println(
      w.calculateSomething());
}

这篇关于在Java中的列表中存储不同类型的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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