1

I found the code below from this SO question. It has worked great so far, but I have come across the need to shuffle arrays of strings. The code below only accepts arrays of ints as a parameter. I am still very new to java and I can't seem to figure out how to let the method accept multiple datatypes in it's parameters. It would be wonderful if I could use the same method to shuffle arrays of ints AND arrays of strings. Can anyone help?

static int[] shuffle(int[] ar) {
    // If running on Java 6 or older, use `new Random()` on RHS here
    Random rnd = ThreadLocalRandom.current();
    for (int i = ar.length - 1; i > 0; i--)
    {
        int index = rnd.nextInt(i + 1);
        // Simple swap
        int a = ar[index];
        ar[index] = ar[i];
        ar[i] = a;
    }
    return ar;
}
Community
  • 1
  • 1
ShoeLace1291
  • 4,551
  • 12
  • 45
  • 81
  • What are you trying to do? `int[]` can only hold `int` values. `String[]` can only hold `String` values. – OneCricketeer Oct 27 '16 at 17:16
  • See http://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html – jthort Oct 27 '16 at 17:16
  • 1
    [Your question is answered here](http://stackoverflow.com/a/34007580/2308683)... Shuffling generic arrays – OneCricketeer Oct 27 '16 at 17:18
  • But what if I don't want to use a list? I want to use an array of strings. – ShoeLace1291 Oct 27 '16 at 17:19
  • You can always wrap an array of strings in a `List` object using `Arrays.asList(arrayOfString)`. Shuffling the list will then shuffle the underlying array. – Ted Hopp Oct 27 '16 at 17:26
  • @TedHopp See his comment before, he doesn't want to use a `List`. – jthort Oct 27 '16 at 17:29
  • 1
    @jthort - Yes, I saw that and my impression was that OP considered a `List` and an array to be incompatible data structures. I was just pointing out that the wrapper provided by `Arrays.asList()` bridges the two. – Ted Hopp Oct 27 '16 at 18:21

6 Answers6

2

You should have different methods for different parameters. This will be method overloading.

In your case (for strings and int)

static int[] shuffle(int[] array)
static String[] shuffle(String[] array)

Or you can have Object array in argument of your method. That way you have to define your int array as Integer class array in calling method.

static Object[] shuffle(Object[] array)

Object is the superclass of all classes in Java and subclass' object can be handled by superclass' reference.

Since int is primitive type, not a class, you have to define int array as Integer array. Integer is type wrapper class for int.

RamenChef
  • 5,557
  • 11
  • 31
  • 43
R. Mittal
  • 126
  • 5
1

You can have two different methods. One that shuffles arrays of ints and one that shuffles arrays of strings.

static String[] shuffleStringArray(String[] ar){
    //method logic goes here
}

The basic structure of your method shouldn't change too much.

Good luck!

Ph0bi4
  • 13
  • 1
  • 3
0

Java solves this problem by making arrays covarient. This means that if B is a subtype of A the B[] is a subtype of A[]. This means that you can make you parameter type Object[] and you can pass in any array of object. For primitive types like int you still need to write a separate method however because primitives are not objects.

puhlen
  • 8,400
  • 1
  • 16
  • 31
0

Use a generic array this way

static <T> T[] shuffle(T[] ar){
// If running on Java 6 or older, use `new Random()` on RHS here
    Random rnd = ThreadLocalRandom.current();
    for (int i = ar.length - 1; i > 0; i--)
    {
      int index = rnd.nextInt(i + 1);
      // Simple swap
      T a = ar[index];
      ar[index] = ar[i];
      ar[i] = a;
    }
    return ar;
}

Invoke it like so:

System.out.println(Arrays.toString(shuffle(new Integer[]{1,2,3,4})));

(or)

System.out.println(Arrays.toString(shuffle(new String[]{"a","b","c"})));
Vasan
  • 4,810
  • 4
  • 20
  • 39
  • Unfortunately, this won't work for arrays of primitives (`int`, `char`, etc.). – Ted Hopp Oct 27 '16 at 17:24
  • 1
    True, but I am not sure if OP absolutely wanted to use primitives or just to shuffle some integers (an Integer array will do fine for this). – Vasan Oct 27 '16 at 17:25
0

You can't have a method perform the same operations on an int array and a reference type array, because Java treats those as completely different types, and their common superclass (Object) lacks functionality as an array. However, you seem to be getting at overloading (see Overloading Methods part). You can have a seperate implementation of the method for different parameters. Simply copy/paste your code and replace relevant instances of int with String. This is actually what is done for the primitive type arrays in Arrays.copyOf.

Anatoly Shamov
  • 2,608
  • 1
  • 17
  • 27
RamenChef
  • 5,557
  • 11
  • 31
  • 43
0

You cannot do that if you need to support both objects and primitives. You need to create an overloaded method for each primitive type and one for Object. Each version of this method will re-implement the same algorithm with diferent data types.

Your method is analogous to the ones in java.util.Arrays class. Take a look at Arrays.java source code from Open JDK.

http://hg.openjdk.java.net/jdk8u/jdk8u/jdk/file/be44bff34df4/src/share/classes/java/util/Arrays.java

Leonardo Cruz
  • 1,189
  • 9
  • 16