Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>java.util.Collections.shuffle(java.util.Arrays.asList(a:_*)) </code></pre> <p>For the above to work correctly, a's element type has to be a subclass of scala.AnyRef (equivalent to java.lang.Object) because Arrays.asList() uses the array passed in as the backing store for the result java.util.List and java.util.List can contain only object references (not primitive values).*</p> <p>That is also the reason why Collections.shuffle() which shuffles the passed-in java.util.List actually shuffled the array.*</p> <p>*: See the note below</p> <p>For example:</p> <pre><code>scala&gt; val a = Array[java.lang.Integer](1, 2, 3) // note the type parameter a: Array[java.lang.Integer] = Array(1, 2, 3) scala&gt; java.util.Collections.shuffle(java.util.Arrays.asList(a:_*)) scala&gt; a res43: Array[java.lang.Integer] = Array(1, 3, 2) scala&gt; java.util.Collections.shuffle(java.util.Arrays.asList(a:_*)) scala&gt; a res45: Array[java.lang.Integer] = Array(1, 2, 3) </code></pre> <p>Note: Scala 2.7.5 is used for the above code snippets. Scala 2.8.0 exhibits different behaviors as Daniel demonstrated. To be on the safe side, do not depend on the fact that the array gets shuffled but instead the list that is returned from Arrays.asList() gets shuffled.</p> <pre><code>scala&gt; val a = Array[java.lang.Integer](1, 2, 3) a: Array[java.lang.Integer] = Array(1, 2, 3) scala&gt; val b = java.util.Arrays.asList(a:_*) b: java.util.List[java.lang.Integer] = [1, 2, 3] scala&gt; java.util.Collections.shuffle(b) scala&gt; b res50: java.util.List[java.lang.Integer] = [2, 1, 3] scala&gt; java.util.Collections.shuffle(b) scala&gt; b res52: java.util.List[java.lang.Integer] = [3, 1, 2] </code></pre>
    singulars
    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. 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