为什么在Java中的List中元素添加不正确?

huangapple go评论77阅读模式
英文:

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&lt;int[]&gt; 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(&quot;no repetitions:&quot;);
        p1(points, POINTS_ON_LINE);
    }

    public static void p1(int[] arr, int pointsOnLine) {
        List&lt;int[]&gt; combinations = new ArrayList&lt;&gt;();

        int lengthArray = arr.length, i;
        int[] index = new int[pointsOnLine];
        int[] temp = new int[pointsOnLine];

        for (i = 0; i &lt; pointsOnLine; i++) {
            index[i] = i;
        }

        if (pointsOnLine &lt; lengthArray) {
            for (int j : index) {
                temp[j] = arr[j];
            }

            boolean flag;
            while (true) {
                System.out.println(&quot;Add array: &quot; + Arrays.toString(temp));
                combinations.add(temp);

                flag = false;

                for (i = pointsOnLine - 1; i &gt;= 0; i--) {
                    if (index[i] != i + lengthArray - pointsOnLine) {
                        flag = true;
                        break;
                    }
                }

                if (!flag) {
                    break;
                }

                index[i] += 1;

                for (int j = i + 1; j &lt; pointsOnLine; j++) {
                    index[j] = index[j - 1] + 1;
                }

                for (i = 0; i &lt; pointsOnLine; i++) {
                    temp[i] = arr[index[i]];
                }
            }
            System.out.println(&quot;End&quot;);
        }
        System.out.println(&quot;Result&quot;);
        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.

huangapple
  • 本文由 发表于 2020年8月24日 01:54:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63550291.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定