Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 309115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:42:44+00:00 2026-05-12T07:42:44+00:00

I have an array that I want to permutate randomly. In Java, there is

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T07:42:44+00:00Added an answer on May 12, 2026 at 7:42 am
    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]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 203k
  • Answers 203k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Visit the information_schema like this select * from information_schema.columns where… May 12, 2026 at 8:30 pm
  • Editorial Team
    Editorial Team added an answer The value 0200 is an octal (base 8) constant. It… May 12, 2026 at 8:30 pm
  • Editorial Team
    Editorial Team added an answer From your page you can do var checkboxes = (CheckBoxList)userControl1.FindControl("checkBoxList1");… May 12, 2026 at 8:30 pm

Related Questions

Assuming that I have an array of int T, I am looking for an
Firstly, an apology for the length of this post. If brevity is the soul
I have an array of custom objects. MyCustomArr[]. I want to convert this to
I have a web-app-database 3 tier server setup. Web requests data from app, and

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.