Java function working with objects and primitives

I am trying to write a function that takes an array and returns the array in a different order.

It works with int, double, string, object, or any object in the array.

Follow the function example for String.

public String[] randomize(String[] array, int randomKey)
{
    //algorithm here
    return newStringArray;
}

For all types of objects, I know I can do it like

public Object[] randomize(Object[] array, int randomKey)
{
    //algorithm here
    return newObjectArray;
}

Instead of boxing the entire primitive type into an object, is there any other method, can I just write the algorithm once and without creating a function for each primitive array? Is there a generic array type that works for AND objects?

thank

PS Do not want to use autoboxing due to a penalty for performance. Quote from [autoboxing]: http://docs.oracle.com/javase/1.5.0/docs/guide/language/autoboxing.html

.

+4
6

, , , . -. , , . , , Arrayys.sort. . Array, ArrayList, Array, get/set . Array , .

+3

, ArrayList. ( -, Integer) .

0

, , , , , - Java API, , , Arrays, Arrays.copyOf .

0

, , :

public Object randomize(Object array, int randomKey) {
  if (array.getClass().isArray()) {
    int length = Array.getLength(array);
    for (int i = 0; i < length; i ++) {
      Object arrayElement = Array.get(array, i);
      // do something with array
    }
    return // result
  } else {
    throw new IllegalArgumentException();
  }
}

, int[] Object.

, .

public int[] randomize(int[] array, int randomKey) {
  return toIntArray(randomize(toIntegerArray(array), randomKey));
}

Java-, JVM. JVMLS . .

0

, AND?

.

- , . autoboxing Wrapper.

0

: .

. Java- ?

, - , , int. , Java - (... )

, "--", . AFAIK, .

0

All Articles