views:

2214

answers:

3

I have an array that I want to permutate randomly. In Java, there is a method Collections.shuffle() that can shuffle the elements of a List randomly. It can be used on an array too:

String[] array = new String[]{"a", "b", "c"};

// Shuffle the array; works because the list returned by Arrays.asList() is backed by the array
Collections.shuffle(Arrays.asList(array));

I tried using this on a Scala array, but the Scala interpreter responds with a lengthy answer:

scala> val a = Array("a", "b", "c")
a: Array[java.lang.String] = Array(a, b, c)

scala> java.util.Collections.shuffle(java.util.Arrays.asList(a))
<console>:6: warning: I'm seeing an array passed into a Java vararg.
I assume that the elements of this array should be passed as individual arguments to the vararg.
Therefore I follow the array with a `: _*', to mark it as a vararg argument.
If that's not what you want, compile this file with option -Xno-varargs-conversion.
       java.util.Collections.shuffle(java.util.Arrays.asList(a))
                                                             ^
<console>:6: error: type mismatch;
 found   : Array[java.lang.String]
 required: Seq[Array[java.lang.String]]
       java.util.Collections.shuffle(java.util.Arrays.asList(a))
                                                             ^

What exactly is happening here? I don't want to compile my code with a special flag (-Xno-varargs-conversion), if that is the solution at all, just because of this.

So, how do I use Java's Collections.shuffle() on a Scala array?

I wrote my own shuffle method in Scala in the meantime:

// Fisher-Yates shuffle, see: http://en.wikipedia.org/wiki/Fisher–Yates_shuffle
def shuffle[T](array: Array[T]): Array[T] = {
 val rnd = new java.util.Random
 for (n <- Iterator.range(array.length - 1, 0, -1)) {
  val k = rnd.nextInt(n + 1)
  val t = array(k); array(k) = array(n); array(n) = t
 }
 return array
}

It shuffles the array in place, and returns the array itself for convenience.

+4  A: 
java.util.Collections.shuffle(java.util.Arrays.asList(a:_*))

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).*

That is also the reason why Collections.shuffle() which shuffles the passed-in java.util.List actually shuffled the array.*

*: See the note below

For example:

scala> val a = Array[java.lang.Integer](1, 2, 3) // note the type parameter                  
a: Array[java.lang.Integer] = Array(1, 2, 3)

scala> java.util.Collections.shuffle(java.util.Arrays.asList(a:_*))

scala> a
res43: Array[java.lang.Integer] = Array(1, 3, 2)

scala> java.util.Collections.shuffle(java.util.Arrays.asList(a:_*))

scala> a
res45: Array[java.lang.Integer] = Array(1, 2, 3)

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.

scala> val a = Array[java.lang.Integer](1, 2, 3)
a: Array[java.lang.Integer] = Array(1, 2, 3)

scala> val b = java.util.Arrays.asList(a:_*)
b: java.util.List[java.lang.Integer] = [1, 2, 3]

scala> java.util.Collections.shuffle(b)

scala> b
res50: java.util.List[java.lang.Integer] = [2, 1, 3]

scala> java.util.Collections.shuffle(b)

scala> b
res52: java.util.List[java.lang.Integer] = [3, 1, 2]
Walter Chang
Thanks. But that doesn't work for a Scala array with integers - it results in a ClassCastException, because the array can't be cast to a Java array of Objects.
Jesper
@Jesper For the Collections.shuffle() to work, make sure the array contains object references and not primitive values. (scala.Int -> java.lang.Integer, scala.Long -> java.lang.Long, etc) I've edited the answer to emphasize this point.
Walter Chang
+4  A: 

To answer the "what exactly is happening here?" part:

When you say java.util.Arrays.asList(a) you're calling a static Java method which is defined to take a variable number of arguments (the vararg ... syntax in Java):

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

In Scala when you make the call to asList you're passing in a single Array[String] and not three string parameters. Even though Scala represents T* as Array[T] you need to tell the compiler that you really mean to be passing three parameters, rather than a single parameter which is a List of three things.

Scala has a convenient way to convert your Array[String] to String,String,String: you use the _* symbol as shown in Walter Chang's answer. You can use it whenever you're passing something to a vararg function.

This is outlined on pages 188 and 189 of Programming in Scala.

You'll also see _* in pattern matching to match zero or more elements in a List.

Richard Dallaway
A: 

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 new array when passing vararg arguments to Java, therefore making the aforementioned example useless.

scala> val b = java.util.Arrays.asList(a: _*)
b: java.util.List[java.lang.String] = [a, b, c]

scala> java.util.Collections.shuffle(b); println(a.toString+" "+b.toString)
Array(a, b, c) [a, b, c]

scala> java.util.Collections.shuffle(b); println(a.toString+" "+b.toString)
Array(a, b, c) [c, b, a]

scala> java.util.Collections.shuffle(b); println(a.toString+" "+b.toString)
Array(a, b, c) [a, c, b]

scala> java.util.Collections.shuffle(b); println(a.toString+" "+b.toString)
Array(a, b, c) [b, a, c]

scala> java.util.Collections.shuffle(b); println(a.toString+" "+b.toString)
Array(a, b, c) [a, b, c]

scala> java.util.Collections.shuffle(b); println(a.toString+" "+b.toString)
Array(a, b, c) [c, a, b]

It does works with Ints, despite the claim otherwise, though:

scala> val a = Array(1,2,3)
a: Array[Int] = Array(1, 2, 3)

scala> val b = java.util.Arrays.asList(a: _*)
b: java.util.List[Int] = [1, 2, 3]

scala> java.util.Collections.shuffle(b); println(a.toString+" "+b.toString)
Array(1, 2, 3) [2, 3, 1]

scala> java.util.Collections.shuffle(b); println(a.toString+" "+b.toString)
Array(1, 2, 3) [3, 2, 1]

scala> java.util.Collections.shuffle(b); println(a.toString+" "+b.toString)
Array(1, 2, 3) [3, 2, 1]

scala> java.util.Collections.shuffle(b); println(a.toString+" "+b.toString)
Array(1, 2, 3) [1, 2, 3]

On Scala 2.8, there's a simpler way:

scala> scala.util.Random.shuffle(a)
res32: Sequence[Int] = Array(1, 2, 3)

scala> scala.util.Random.shuffle(a)
res33: Sequence[Int] = Array(2, 1, 3)

scala> scala.util.Random.shuffle(a)
res34: Sequence[Int] = Array(3, 2, 1)

Note that, in a Scala fashion, it does not change the original array.

Daniel
Daniel, if I do this: val a = Array("a", "b", "c") java.util.Collections.shuffle(java.util.Arrays.asList(a: _*))then my Scala array does get shuffled. And if I make a an array of Ints instead of Strings, I get a ClassCastException.
Jesper
And thanks for the tip about Scala 2.8, so there will be a shuffle() method in the library in 2.8. Ofcourse it returns a new array, that's the typical functional way of programming.
Jesper
@Daniel I think you are using 2.8.0 repl. Try your examples in 2.7.5 and you will see differences from your results. Apparently, 2.8.0 generates a new Seq when it sees ":_*" but 2.7.5 just wraps a Seq around the array.
Walter Chang
I *am* using a 2.8.0 REPL, and not a particularly up-to-date one at that. From your answer, I gathered there was a difference. I'm just wondering if it merits a bug on bug-traq.
Daniel
I haven't found an open bug, so I openned 2250 about it.
Daniel