Reputation: 4698
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;
}
Upvotes: 1
Views: 157
Reputation: 5558
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
.
Upvotes: 0
Reputation: 1189
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
Upvotes: 0
Reputation: 126
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.
Upvotes: 2
Reputation: 13
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!
Upvotes: 1
Reputation: 4956
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"})));
Upvotes: 0
Reputation: 8529
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.
Upvotes: 0