怎么做?

以下是尝试的代码。


[尝试代码]

package com.company;

import java.util.ArrayList;
import java.util.Date;

public class Main {
    public static void main(String[] args) {
        ArrayList<String, Integer, Date> myArray = new ArrayList<String, Integer, Date>();

        myArray.add("FIRST");
        myArray.add("SECOND");
        myArray.add("THIRD");

        // add multiple object
        myArray.add(new Integer(10));

        // add multiple object
        myArray.add(new Date());

    }
}

最佳答案

下面的行甚至不会编译。

ArrayList<String, Integer, Date> myArray = new ArrayList<String, Integer, Date>();


您可以尝试使用以下方法来实现

ArrayList<Object> myArray = new ArrayList<>(); // Object type


但完全不建议这样做。从ArrayList取回值时,您需要手动处理不同类型的对象,这也很麻烦。

因此,它总是创建要使用的特定类型的ArrayList

ArrayList<String> myString = new ArrayList<>(); // For strings
ArrayList<Date> myDates = new ArrayList<>(); // For Date

10-07 19:12
查看更多