Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It seems Scala is doing something different from Java when it comes to varargs. At least, I can't get that array shuffled any way I try. Supposedly, the shuffle on the list would shuffle the array because the list is array-backed. Well, it seems that Scala will create a <em>new</em> array when passing vararg arguments to Java, therefore making the aforementioned example useless.</p> <pre><code>scala&gt; val b = java.util.Arrays.asList(a: _*) b: java.util.List[java.lang.String] = [a, b, c] scala&gt; java.util.Collections.shuffle(b); println(a.toString+" "+b.toString) Array(a, b, c) [a, b, c] scala&gt; java.util.Collections.shuffle(b); println(a.toString+" "+b.toString) Array(a, b, c) [c, b, a] scala&gt; java.util.Collections.shuffle(b); println(a.toString+" "+b.toString) Array(a, b, c) [a, c, b] scala&gt; java.util.Collections.shuffle(b); println(a.toString+" "+b.toString) Array(a, b, c) [b, a, c] scala&gt; java.util.Collections.shuffle(b); println(a.toString+" "+b.toString) Array(a, b, c) [a, b, c] scala&gt; java.util.Collections.shuffle(b); println(a.toString+" "+b.toString) Array(a, b, c) [c, a, b] </code></pre> <p>It does works with Ints, despite the claim otherwise, though:</p> <pre><code>scala&gt; val a = Array(1,2,3) a: Array[Int] = Array(1, 2, 3) scala&gt; val b = java.util.Arrays.asList(a: _*) b: java.util.List[Int] = [1, 2, 3] scala&gt; java.util.Collections.shuffle(b); println(a.toString+" "+b.toString) Array(1, 2, 3) [2, 3, 1] scala&gt; java.util.Collections.shuffle(b); println(a.toString+" "+b.toString) Array(1, 2, 3) [3, 2, 1] scala&gt; java.util.Collections.shuffle(b); println(a.toString+" "+b.toString) Array(1, 2, 3) [3, 2, 1] scala&gt; java.util.Collections.shuffle(b); println(a.toString+" "+b.toString) Array(1, 2, 3) [1, 2, 3] </code></pre> <p>On Scala 2.8, there's a simpler way:</p> <pre><code>scala&gt; scala.util.Random.shuffle(a) res32: Sequence[Int] = Array(1, 2, 3) scala&gt; scala.util.Random.shuffle(a) res33: Sequence[Int] = Array(2, 1, 3) scala&gt; scala.util.Random.shuffle(a) res34: Sequence[Int] = Array(3, 2, 1) </code></pre> <p>Note that, in a Scala fashion, it does not change the original array.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload