如何使用Java计算ArrayList Model的raw和colomn

时间:2017-06-20 11:15:38

标签: java arrays arraylist multidimensional-array

我问问题,我有模型数组列表,但我希望模型数组列表中的值转换或添加到java中的ArrayList 2维度。?

我的代码:

班级Abc:

public class Ebd {
    public static void main (String [] args){
        List<Abc> itemAbc = new ArrayList<Abc>();
        Abc model1 = new Abc();
        model1.setValue1(6);
        model1.setValue2(4);
        model1.setValue3(3);
        model1.setValue4(5);
        itemAbc.add(model1);

        model1.setValue1(3);
        model1.setValue2(1);
        model1.setValue3(6);
        model1.setValue4(4);
        itemAbc.add(model1);
    }
}

班级主要:

R = 6,4,3,5

    3,1,6,4

itemAbc =&gt; int a [] [] ..... ??

F = {{6,4,3,5},{3,1,6,4}}

我想要矩阵:

=IFNA(IF(H2=VLOOKUP(H2|C:C|1|FALSE)|TRUE|FALSE)|FALSE)

你可以得到R(1,1)= 6。

如何将值数组列表itemAbc发送到Array 2 dimensi。?

感谢。

1 个答案:

答案 0 :(得分:1)

 1. Following code will convert your list to two dimensional array.

    private  int[][] to2DArray(List<Abc> itemAbc){
        int[][] a  = null;
        if(itemAbc!=null){
            a = new  int[itemAbc.size()][4];
            int i = 0;
            for(Abc abc : itemAbc){
                a[i][0] = abc.getValue1();
                a[i][1] = abc.getValue2();
                a[i][2] = abc.getValue3();
                a[i][3] = abc.getValue4();
                i++;
            }
        }
        return a;
    }

 2. You are missing initialization of Abc object after adding first object .

            List<Abc> itemAbc = new ArrayList<Abc>();
            Abc model1 = new Abc();
            model1.setValue1(6);
            model1.setValue2(4);
            model1.setValue3(3);
            model1.setValue4(5);
            itemAbc.add(model1);
            model1 =new Abc();// line you are missing.
            model1.setValue1(3);
            model1.setValue2(1);
            model1.setValue3(6);
            model1.setValue4(4);
            itemAbc.add(model1);