【问题标题】:Collections.shuffle(a) magic? [duplicate]Collections.shuffle(a) 魔法? [复制]
【发布时间】:2016-08-01 10:41:32
【问题描述】:

魔法之后
System.out.println("*****"); Collections.shuffle(a);

~$ java -version java版本“1.8.0_101” Java(TM) SE 运行时环境 (build 1.8.0_101-b13) Java HotSpot(TM) 64 位服务器 VM(内部版本 25.101-b13,混合模式)

public static void main(String[] args) {

        List<Integer> a = Arrays.asList(1,  3,  5, 9, 14);
        Collections.shuffle(a);

        System.out.println(a);

        List<Integer> b = a.subList(0, a.size() - 1);
        List<Integer> b1 = new ArrayList<>(a.subList(0, a.size() - 1));
        System.out.println(b);
        System.out.println(b1);

        System.out.println("*****");
        Collections.shuffle(a);
        System.out.println(a);

        Collections.shuffle(b);
        System.out.println(b);

        Collections.shuffle(b1);
        System.out.println(b1);
}

[14, 1, 9, 3, 5]
[14, 1, 9, 3]
[14, 1, 9, 3]
*****
[14, 9, 3, 5, 1]
[3, 14, 9, 5]
[1, 14, 9, 3]

shuffle(a) 之后的 b 不是 a.subList(0, a.size() - 1) 之后的 eq b; \

更新

是的!一步步! 1) 创建 a 2) 创建 b 子列表 a 3) shuffle(a) 为什么 shuffle(a) 修改 b ?

更新 2

为什么是正确的?为什么当我 shuffle b 我改变 a 的顺序时这是真的?

    public static void main(String[] args) {
        List<Integer> a = Arrays.asList(1,  3,  5, 9, 14);
        Collections.shuffle(a);
        System.out.println(a);
        List<Integer> b = a.subList(0, a.size() - 1);
        System.out.println(b);
        System.out.println("*****");
        Collections.shuffle(b);
        System.out.println(a);



        System.exit(0);
}

[1, 9, 3, 5, 14]
[1, 9, 3, 5]
*****
[3, 9, 5, 1, 14]

【问题讨论】:

  • 我真的不太清楚你的意思 - 你的最后一句话没有准确地解释你的期望或更重要的是为什么你期望它。
  • 到底是什么问题?你想知道为什么洗牌实际上是洗牌集合吗?我的意思是......这是它应该做的......
  • 您似乎有三个独立的列表,并且您分别对每个列表进行了洗牌。
  • 其实它们并不是独立的。 ba 的子列表,因此它由同一个 ArrayList 实例支持。
  • 请注意,您在打印之前将bb1 改组,因此即使if 它们会保留a 的相同顺序,那么它们将以不同的顺序打印洗牌后。

标签: java java-8


【解决方案1】:

ba view on the first 4 elements of a。如果这些元素在a 中发生变化,它们也会在b 中发生变化。

所以你有,最初:

  • a = {14, 1, 9, 3, 5}
  • b = {14, 1, 9, 3};

洗牌a后,你有:

  • a = {14, 9, 3, 5, 1}
  • b = {14, 9, 3, 5}

另一方面,b1 是一个独立的列表,在创建b1 后对a 所做的更改对b1 没有影响。

【讨论】:

【解决方案2】:

b 在 shuffle(a) 之后不是 eq b 在 a.subList(0, a.size() - 1);

那是因为你在洗牌a 之后也洗牌了b1b,因此bb1 都不会显示你的预期。相反,您应该a 进行洗牌,以查看b,作为a 子列表的视图,采用相同的顺序,而b1,作为一个新列表,保持独立a 的重新排序。

List<Integer> a = Arrays.asList(1,  3,  5, 9, 14);
List<Integer> b = a.subList(0, a.size() - 1);
List<Integer> b1 = new ArrayList<>(a.subList(0, a.size() - 1));
System.out.println(a);
System.out.println(b);
System.out.println(b1);

System.out.println("*****");
Collections.shuffle(a); // shuffle a, but do not shuffle b and b1!
System.out.println(a);
System.out.println(b);
System.out.println(b1);

输出:

[1, 3, 5, 9, 14]
[1, 3, 5, 9]
[1, 3, 5, 9]
*****
[5, 14, 1, 9, 3]
[5, 14, 1, 9]
[1, 3, 5, 9]

【讨论】:

  • 是的!一步步! 1) 创建 a 2) 创建 b 子列表 a 3) shuffle(a) 为什么 shuffle(a) 修改 b ?
  • thx List subList(int fromIndex, int toIndex) 返回此列表在指定 fromIndex(包括)和 toIndex(不包括)之间部分的视图。 (如果 fromIndex 和 toIndex 相等,则返回列表为空。)返回列表由此列表支持,因此返回列表中的非结构性更改会反映在此列表中,反之亦然。返回的列表支持此列表支持的所有可选列表操作。
猜你喜欢
  • 2014-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-24
  • 1970-01-01
相关资源
最近更新 更多