java 中的数据类型和动态绑定

Data Type and Dynamic Binding in java

我从文件中读取了一个矩阵,但所有列的数据类型都不同。 我找不到一个结构来保存和操作我的数据。感谢您的帮助。

    // I read a matrix from file and all column have a different type.
    int[]    iT = new int[] {1,3,5};
    long[]   lT = new long[] {123, 456, 789};
    double[] dT = new double[] {1.2d, 3.2d, 5.2d};

    // I like to know if there are a kind of structure to hold and manipulate it.
    Collection<Object[]> collection = new HashSet<Object[]>();

    collection.add(iT);
    collection.add(dT);
    collection.add(lT);     

    for(Object[] obj : collection) {

        String type = obj.getClass().getSimpleName();

        switch (type) {

        case "double[]":
            for(Object element : obj) System.out.println(element);
            break;

        case "int[]":
            for(Object element : obj) System.out.println(element);
            break;

        case "long[]":
            for(Object element : obj) System.out.println(element);
            break;
        }
    }

根据我对您的任务的理解,您希望单个集合中的所有单个值,而不是数组(如果我错了请纠正我)。您基本上可以将它们放入(几乎)任何您喜欢的集合中(我使用过 ArrayList),您遇到的问题是原始数组需要装箱才能将它们添加到您的集合中:

public static void main(String[] args) {
    int[] iT = new int[] { 1, 3, 5 };
    long[] lT = new long[] { 123, 456, 789 };
    double[] dT = new double[] { 1.2d, 3.2d, 5.2d };

    Integer[] boxedInts = IntStream.of(iT).boxed().toArray(Integer[]::new);
    Long[] boxedLongs = LongStream.of(lT).boxed().toArray(Long[]::new);
    Double[] boxedDoubles = DoubleStream.of(dT).boxed().toArray(Double[]::new);

    Collection<Object> collection = new ArrayList<>();

    collection.addAll(Arrays.asList(boxedInts));
    collection.addAll(Arrays.asList(boxedLongs));
    collection.addAll(Arrays.asList(boxedDoubles));

    for (Object element : collection) {
        System.out.print(element.toString() + " ");
    }
    //prints 1 3 5 123 456 789 1.2 3.2 5.2 
}