英文:
Why elements are added incorrectly in List<int[]> in Java?
问题
关于问题[1],用户@deadshot给了我答案。
我决定稍微改变程序。
我添加了一个对象 - ArrayList<int[]> combinations,它将存储带有数字的int[]数组(此数组的大小始终为3)。
我使用add()方法将数组(array[])添加到combinations中。
package com.company;
import java.util.*;
public class Main {
    static int POINTS_ON_LINE = 3;
    public static void main(String[] args) {
        int[] points = new int[]{1, 2, 3, 4};
        System.out.println("无重复:");
        p1(points, POINTS_ON_LINE);
    }
    public static void p1(int[] arr, int pointsOnLine) {
        List<int[]> combinations = new ArrayList<>();
        int lengthArray = arr.length, i;
        int[] index = new int[pointsOnLine];
        int[] temp = new int[pointsOnLine];
        for (i = 0; i < pointsOnLine; i++) {
            index[i] = i;
        }
        if (pointsOnLine < lengthArray) {
            for (int j : index) {
                temp[j] = arr[j];
            }
            boolean flag;
            while (true) {
                System.out.println("添加数组:" + Arrays.toString(temp));
                combinations.add(temp);
                flag = false;
                for (i = pointsOnLine - 1; i >= 0; i--) {
                    if (index[i] != i + lengthArray - pointsOnLine) {
                        flag = true;
                        break;
                    }
                }
                if (!flag) {
                    break;
                }
                index[i] += 1;
                for (int j = i + 1; j < pointsOnLine; j++) {
                    index[j] = index[j - 1] + 1;
                }
                for (i = 0; i < pointsOnLine; i++) {
                    temp[i] = arr[index[i]];
                }
            }
            System.out.println("结束");
        }
        System.out.println("结果");
        for(int[] q : combinations) {
            System.out.println(Arrays.toString(q));
        }
    }
}
为什么结果是这样的:
无重复:
添加数组:[1, 2, 3]
添加数组:[1, 2, 4]
添加数组:[1, 3, 4]
添加数组:[2, 3, 4]
结束
结果:
[2, 3, 4]
[2, 3, 4]
[2, 3, 4]
[2, 3, 4]
但是对象combinations应该包含这些内容:
[1, 2, 3]
[1, 2, 4]
[1, 3, 4]
[2, 3, 4]
英文:
On my question user @deadshot give me answer.
I decided to little change the program.
I add one object - ArrayList<int[]> combinations, which will store array int[] with numbers (size this array is always 3).
I add to combinations arrays (array[]) using the method add().
package com.company;
import java.util.*;
public class Main {
    static int POINTS_ON_LINE = 3;
    public static void main(String[] args) {
        int[] points = new int[]{1, 2, 3, 4};
        System.out.println("no repetitions:");
        p1(points, POINTS_ON_LINE);
    }
    public static void p1(int[] arr, int pointsOnLine) {
        List<int[]> combinations = new ArrayList<>();
        int lengthArray = arr.length, i;
        int[] index = new int[pointsOnLine];
        int[] temp = new int[pointsOnLine];
        for (i = 0; i < pointsOnLine; i++) {
            index[i] = i;
        }
        if (pointsOnLine < lengthArray) {
            for (int j : index) {
                temp[j] = arr[j];
            }
            boolean flag;
            while (true) {
                System.out.println("Add array: " + Arrays.toString(temp));
                combinations.add(temp);
                flag = false;
                for (i = pointsOnLine - 1; i >= 0; i--) {
                    if (index[i] != i + lengthArray - pointsOnLine) {
                        flag = true;
                        break;
                    }
                }
                if (!flag) {
                    break;
                }
                index[i] += 1;
                for (int j = i + 1; j < pointsOnLine; j++) {
                    index[j] = index[j - 1] + 1;
                }
                for (i = 0; i < pointsOnLine; i++) {
                    temp[i] = arr[index[i]];
                }
            }
            System.out.println("End");
        }
        System.out.println("Result");
        for(int[] q : combinations) {
            System.out.println(Arrays.toString(q));
        }
    }
}
Why is the result like this:
no repetitions:
Add array: [1, 2, 3]
Add array: [1, 2, 4]
Add array: [1, 3, 4]
Add array: [2, 3, 4]
End
Result:
[2, 3, 4]
[2, 3, 4]
[2, 3, 4]
[2, 3, 4]
Why object combinations contain:
[2, 3, 4]
[2, 3, 4]
[2, 3, 4]
[2, 3, 4]
But object combinations should be contain this:
[1, 2, 3]  
[1, 2, 4]  
[1, 3, 4]  
[2, 3, 4] 
答案1
得分: 0
将这行代码更改为:
combinations.add(Arrays.stream(temp).toArray());
这将获取数组在其当前状态下的副本,并将其添加到列表中。这使用了Java 8中的流式API。
英文:
Change this line:
combinations.add(temp); 
To this:
combinations.add(Arrays.stream(temp).toArray());
This will get a copy of the array at it's current state, and add it to the List. This is using the stream api from java 8.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论