有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java ArrayList向第三层添加元素

我在一个arraylist中循环,试图添加我从ai中得到的每个答案。得到(k)。加(分)得(k)。得到(百万分)。得到(k)。得到(l));进入各自的arraylist of arraylist of arraylist。对于每三个元素,它应该将以下元素添加到新的arraylist中。此数据集中有12个元素,因此它应该看起来像[[[0,1,2],[3,4,5],[6,7,8],[9,10,11],[],[],[],[],[],[],[],[],[],[],[],[]

ArrayList<ArrayList> ai = new ArrayList<ArrayList>();
        // Creates arrays based on number of centroids
        for (int k = 0; k < numCen; k++) {
            ai.add(new ArrayList(k));
        }

        for (int i = 0; i < numCen; i++) {
            for(int k = 0; k < dim; k++) {
            ai.get(i).add(new ArrayList(k));
            //ai.add(new ArrayList(i));
            }
        }

        System.out.println(ai);     


        // Gets all of the centroids
            for (int k = 0; k < numCen; k++) {
                // Gets the centroid itself
                for (int m = 0; m < Cent.size() + 1; m++) {
                    // Gets all the elements in array
                    for (int l = 0; l < Cent.size() + 1; l++) {
                        // Skips over the element being subtracted so you never get 0
                        if(Cent.get(k).get(m) == Cent.get(k).get(l)) {
                            continue;
                        }
                        //Places all the elements in ai
                        ai.get(k).add(Cent.get(k).get(m)-Cent.get(k).get(l));
                    }
                }
                System.out.println(ai);
        }

这是我当前的输出:

[[[], [], [], [], -0.4861111111108659, 0.18691148775888072, 0.18055555555552283, 0.4861111111108659, 0.6730225988697466, 0.6666666666663887, -0.18691148775888072, -0.6730225988697466, -0.006355932203357881, -0.18055555555552283, -0.6666666666663887, 0.006355932203357881], [[], [], [], [], 0.027777777777828083, -0.2504708097928492, -0.2638888888887171, -0.027777777777828083, -0.2782485875706773, -0.29166666666654517, 0.2504708097928492, 0.2782485875706773, -0.013418079095867896, 0.2638888888887171, 0.29166666666654517, 0.013418079095867896], [[], [], [], [], -0.40277777777757906, 0.15442561205268038, 0.1805555555555111, 0.40277777777757906, 0.5572033898302594, 0.5833333333330901, -0.15442561205268038, -0.5572033898302594, 0.02612994350283071, -0.1805555555555111, -0.5833333333330901, -0.02612994350283071]]

预期产出:

[[[-0.4861111111108659, 0.18691148775888072, 0.18055555555552283], 
[0.4861111111108659, 0.6730225988697466, 0.6666666666663887], 
[-0.18691148775888072, -0.6730225988697466, -0.006355932203357881], 
[-0.18055555555552283, -0.6666666666663887, 0.006355932203357881]], 
[[0.027777777777828083, -0.2504708097928492, -0.2638888888887171], 
[-0.027777777777828083, -0.2782485875706773, -0.29166666666654517], 
[0.2504708097928492, 0.2782485875706773, -0.013418079095867896], 
[0.2638888888887171, 0.29166666666654517, 0.013418079095867896]], [[ 
-0.40277777777757906, 0.15442561205268038, 0.1805555555555111], 
[0.40277777777757906, 0.5572033898302594, 0.5833333333330901], 
[-0.15442561205268038, -0.5572033898302594, 0.02612994350283071], 
[-0.1805555555555111, -0.5833333333330901, -0.02612994350283071]]]

共 (1) 个答案

  1. # 1 楼答案

    添加第三层。现在你只有两个

    1. List<Integer>可以存储[1,2]
    2. List<List<Integer>>可以存储{},这就是您现在拥有的
    3. List<List<List<Integer>>>可以存储[ [[1,2],[3,4]] , [[5,6],[7,8]] ]

    相互包装的List<>数量是层数(或尺寸,这是一个通用名称)
    当然,任何List都可以为空,[]


    哦,是的
    因此,您的内部ArrayList包含泛型Object-s,您碰巧用ArrayList填充了它,但仍然很难访问它们,您可以这样做:
    ((ArrayList)ai.get(firstIndex).get(secondIndex)).get/set(thirdIndex)
    

    这是您的代码中没有发生的事情,所以我有点困惑为什么您会有第三层的内容。这是你根本没有表现出来的东西

    无论如何,你真的应该切换到“更类型化”的方法,并且

    List<List<List<Float>>> ai=new ArrayList<>();
    

    一开始