本文介绍了如何在Scala数组上使用Java Collections.shuffle()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组,我想随机排列。在Java中,有一个方法Collections.shuffle()可以随机洗牌的元素。它也可以用在数组上:

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));

我试过在Scala数组上使用这个,但是Scala解释器回应一个冗长的答案: p>

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))
                                                             ^

这里发生了什么?我不想编译我的代码与一个特殊的标志(-Xno-varargs-conversion),如果这是解决方案,正是因为这一点。

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.

在Scala中我写了自己的shuffle方法:

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.

推荐答案

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

为了使上述正常工作,a的元素类型必须是scala.AnyRef(等价于java.lang.Object)的子类,因为Arrays.asList()使用传入的数组作为结果java.util的后备存储.List和java.util.List只能包含对象引用(不是原始值)。*

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

这也是为什么Collections.shuffle -in java.util.List实际改写了数组。*

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

*:见下面的注释

示例:

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)

注意:Scala 2.7.5用于上述代码片段。 Scala 2.8.0展示了不同的行为,如Daniel所示。为了安全起见,不要依赖于数组被洗牌的事实,而是从Arrays.asList()返回的列表被洗牌。

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]

这篇关于如何在Scala数组上使用Java Collections.shuffle()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 15:33