简体   繁体   English

创建并处理通用类型列表

[英]Create and process a list of generic type

I have a list of values that I need to store that can be of any type (mostly int, double, date, ...) at runtime. 我有一个需要存储的值列表,这些值可以在运行时为任何类型(主要是int,double,date等)。 Later I need to process these. 稍后我需要处理这些。 What is the way I should approach this problem? 我应该怎么处理这个问题?

This is what I have come up with, and it appears to work, but I am not sure if this is the correct way to approach this problem. 这是我想出的,它似乎可以工作,但是我不确定这是否是解决此问题的正确方法。

    ArrayList<Object> list = new ArrayList<Object>();

    list.add("hello");
    list.add(1);
    list.add(0.5);

    for (int i = 0; i < list.size(); i++)
    {
        Object obj = list.get(i);

        if (obj instanceof Integer)
        {
            System.out.println((double)obj);
        }
        else if (obj instanceof String)
        {
            System.out.println(obj.toString());
        }
        else
        {
            System.out.println("Unable to deal with type.");
        }
    }

I realize your example is probably simplified, but I'd introduce a wrapper for all of them to represent whatever is common between them in your world. 我意识到您的示例可能已简化,但是我将为所有这些示例引入一个包装器,以表示您的示例中它们之间的共同点。 Here's an example: 这是一个例子:

interface PrintableThing {
   string specialToString();
}

class IntPrintable implements PrintableThing {
    private final int int_;

    public IntPrintable(int a) { int_ = a; }
    public specialToString() { return ((double)int_).toString(); }
}

And so on. 等等。 Now you can just iterate over a list of ArrayList<PrintableThing> and ask each element to do it's specialToString method. 现在,您可以遍历ArrayList<PrintableThing>的列表,并要求每个元素都执行它的specialToString方法。

Two ways of doing it I can think of: 我可以想到两种方法:

  1. As your provided code shows, check every object with instanceof in else-if's. 如您提供的代码所示,在else-if中使用instanceof检查每个对象

  2. Create an abstract class (or interface) with a set of methods you will apply to objects, eg toString() (which – in many cases – is already implemented the way you want it to be). 使用将应用于对象的一组方法创建一个抽象类(或接口) ,例如toString() (在许多情况下,该方法已经按照您希望的方式实现了)。

additionally you can create a custom javabean class to hold these data types . 另外,您可以创建一个自定义javabean类来保存这些数据类型。 Like this : 像这样 :

class data{
    private Double double1;
    private int int1;
    private String1;
}

and you can make a list of this class type : 您可以列出此类的列表:

List<data> l = new List<data>();

In my opinion that makes more sense that your current implementation. 我认为这比您当前的实现更有意义。

Usually you will use the generic Type to make sure that a Collection holds only that Type of elements. 通常,您将使用通用Type来确保Collection仅包含该Type元素。 So it is not necessary to say that the elements of ArrayList have the type Object because that's normal. 因此,不必说ArrayList的元素具有Object类型,因为那是正常的。

And when you convert to other types, make sure you use the type you tested with instanceof otherwise you can encounter a ClassCastException . 并且在转换为其他类型时,请确保使用通过instanceof测试的类型,否则可能会遇到ClassCastException What I mean is oyur line where you converting an Object of Integer to an primitive double. 我的意思是在您的行上,将Integer对象转换为原始双精度型。

Otherwise it's OK 否则没关系

And one hint, instead of using: 还有一个提示,而不是使用:

for (int i = 0; i < list.size(); i++)

you can use 您可以使用

for (Object obj : list)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM