java java数组改组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4334950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 05:56:46  来源:igfitidea点击:

java array shuffling

javaarrays

提问by dimitrisli

I've noticed that the underlying int array is getting changed given the way the list is being created:

我注意到根据创建列表的方式,底层的 int 数组正在发生变化:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;


public class Shuffling {

    static Integer[] intArr = {1, 2, 3, 4, 5};
    static Random random = new Random(7);
    public static void main(String[] args) {

        List<Integer> intList = new ArrayList<Integer>(Arrays.asList(intArr));
        Collections.shuffle(intList, random);
        System.out.println("List after shuffling: " + intList);
        System.out.println("intArr: " + Arrays.toString(intArr));
        //OUTPUT:
        //List after shuffling: [5, 4, 1, 3, 2]
        //intArr: [1, 2, 3, 4, 5]


        List<Integer> intList2 = Arrays.asList(intArr);
        Collections.shuffle(intList2, random);
        System.out.println("List2 after shuffling: " + intList2);
        System.out.println("intArr: " + Arrays.toString(intArr));
        //OUTPUT:
        //List2 after shuffling: [5, 3, 4, 2, 1]
        //intArr: [5, 3, 4, 2, 1]
    }
}

Why is that happening?

为什么会这样?

回答by Sean Patrick Floyd

Arrays.asList()constructs a special list that is backed by the original array.

Arrays.asList()构造一个由原始数组支持的特殊列表。

Which is why the list does not support the (optional) add()and remove()methods from the Collection interface (they wouldn't be possible using an array).

这就是为什么该列表不支持来自 Collection 接口的(可选)add()remove()方法(它们不可能使用数组)。

Interestingly, the returned class is called ArrayList, although it is not to be confused with java.util.ArrayList.

有趣的是,返回的类被称为ArrayList,尽管不要与java.util.ArrayList.

System.out.println(
    Arrays.asList("1", "2", "3")
    .getClass().getName()
);
// Output: java.util.Arrays$ArrayList

回答by Armand

From javadocfor Arrays.asList(T... a):

javadocArrays.asList(T... a)

Returns a fixed-size list backedby the specified array. (Changes to the returned list "write through" to the array.)

返回由指定数组支持的固定大小列表。(更改返回的列表“直写”到数组。)

回答by vladmihaisima

To quote from the API link text:

引用 API链接文本

public static <T> List<T> asList(T... a)

Returns a fixed-size list backed by the specified array. (Changes to the returned list "write through" to the array.)

返回由指定数组支持的固定大小列表。(更改返回的列表“直写”到数组。)

So, any changes you perform on intList2 will have an effect on intArr. The constructor of ArrayList on the other side, will copy the elements, so changes made to the order in intList will not affect intArr (because elements are copied)

因此,您对 intList2 执行的任何更改都会对 intArr 产生影响。另一边的ArrayList的构造函数,会复制元素,所以对intList中顺序的改变不会影响intArr(因为元素被复制了)

回答by JohnGray

List<Integer> intList = new ArrayList<Integer>(Arrays.asList(intArr));
List<Integer> intList = new ArrayList<Integer>(Arrays.asList(intArr));

In intList you got a shallow copy of your array. Shallow copy means, you copied only array itself, not elements it holds. (There is also a deep copy. It means a complete array copy).

在 intList 中,您获得了数组的浅拷贝。浅拷贝意味着,你只拷贝了数组本身,而不是它保存的元素。(还有一个深拷贝,意思是一个完整的数组拷贝)。

List<Integer> intList2 = Arrays.asList(intArr);
List<Integer> intList2 = Arrays.asList(intArr);

In intList2 you got the same array. Not even a shallow copy. Whenever you change intList2, you also modify intArr.

在 intList2 中,您得到了相同的数组。甚至不是浅拷贝。每当您更改 intList2 时,您也会修改 intArr。

回答by karakuricoder

The actual change is in the random seed being passed into the shuffle method. Every time the shuffle method is called it gets the next, sic different, number in from the Random instance.

实际的变化在于随机种子被传递到 shuffle 方法中。每次调用 shuffle 方法时,它都会从 Random 实例中获取下一个不同的数字。