简体   繁体   English

在Java中将ArrayList添加到另一个ArrayList

[英]Adding ArrayLists to another ArrayList in java

I want to find the minimum and the maximum from a matrix from each column. 我想从每一列的矩阵中找到最小值和最大值。 My function 我的功能

public static void findMinMax(double[][] matrix) {
        ArrayList<Double> column = new ArrayList<Double>();
        ArrayList<Double> minmax = new ArrayList<Double>();
        ArrayList<ArrayList<Double>> minMaxValues = new ArrayList<ArrayList<Double>>();
            for (int j=0;j<matrix[1].length;j++) {
                column.clear();
                minmax.clear();
                for (int i=0;i<matrix.length;i++) {
                    column.add(matrix[i][j]);
                }
                double max = Collections.max(column);
                double min = Collections.min(column);
                minmax.add(min);
                minmax.add(max);
                try {
                    System.out.println("adding to final minmax " + j);
                    minMaxValues.add(minmax);
                    System.out.println(minMaxValues);
                } catch (Exception ex) {
                    System.out.println(ex.getMessage());
                }
            }
            System.out.println(minMaxValues);
    }

The problem is that at the last iteration it adds to minMaxValues only the last minmax ArrayList. 问题在于,在最后一次迭代中,它仅将最后一个minmax ArrayList添加到minMaxValues Here is an example of the output 这是输出示例

adding to final minmax 0
[[-3.5, 4.5]]
adding to final minmax 1
[[-3.5, 4.5], [-3.5, 4.5]]
adding to final minmax 2
[[-3.5, 4.5], [-3.5, 4.5], [-3.5, 4.5]]
adding to final minmax 3
[[-3.5, 4.2], [-3.5, 4.2], [-3.5, 4.2], [-3.5, 4.2]]
adding to final minmax 4
[[-3.5, 4.2], [-3.5, 4.2], [-3.5, 4.2], [-3.5, 4.2], [-3.5, 4.2]]
adding to final minmax 5
[[-3.5, 4.2], [-3.5, 4.2], [-3.5, 4.2], [-3.5, 4.2], [-3.5, 4.2], [-3.5, 4.2]]
adding to final minmax 6
[[-3.5, 4.2], [-3.5, 4.2], [-3.5, 4.2], [-3.5, 4.2], [-3.5, 4.2], [-3.5, 4.2], [-3.5, 4.2]]
adding to final minmax 7
[[-3.9, 4.2], [-3.9, 4.2], [-3.9, 4.2], [-3.9, 4.2], [-3.9, 4.2], [-3.9, 4.2], [-3.9, 4.2], [-3.9, 4.2]]
adding to final minmax 8
[[-3.9, 4.2], [-3.9, 4.2], [-3.9, 4.2], [-3.9, 4.2], [-3.9, 4.2], [-3.9, 4.2], [-3.9, 4.2], [-3.9, 4.2], [-3.9, 4.2]]
adding to final minmax 9
[[-3.9, 4.2], [-3.9, 4.2], [-3.9, 4.2], [-3.9, 4.2], [-3.9, 4.2], [-3.9, 4.2], [-3.9, 4.2], [-3.9, 4.2], [-3.9, 4.2], [-3.9, 4.2]]
adding to final minmax 10
[[-3.9, 4.5], [-3.9, 4.5], [-3.9, 4.5], [-3.9, 4.5], [-3.9, 4.5], [-3.9, 4.5], [-3.9, 4.5], [-3.9, 4.5], [-3.9, 4.5], [-3.9, 4.5], [-3.9, 4.5]]

As you can see it works fine until minmax 10 where I don't understand what happens. 如您所见,它在minmax 10之前工作正常,我不知道会发生什么。 Can anyone help me? 谁能帮我?

You need to reinitialize ArrayList<Double> minmax for every iteration. 您需要为每次迭代重新初始化ArrayList<Double> minmax Otherwise your result list will just contain 11 references to the same list , containing the last values that you put in. 否则,结果列表将只包含对该列表的 11个引用,其中包含您输入的最后一个值。

You need to move 你需要搬家

ArrayList<Double> minmax = new ArrayList<Double>();

inside your loop. 在你的循环中。

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

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