1

I wrote this code

<T> T [] reverse(T [] arr) {
  for(int i = 0 ; i < arr.length / 2; i++) {
    T buf = arr[i];
    arr[i] = arr[arr.length - i - 1];
    arr[arr.length - i - 1] = buf;
  }
  return arr;
}

But when I pass an array with primitive type like that int [] arr = {0,1,2,3}; the console return method reverse in class Rope cannot be applied to given types; When I write the method directly in Processing the array of int is accepted, but when I implement my function in my library java, that's don't work. I'll suppose there is a solution to deal between int and Integer but how ?

Knupel
  • 323
  • 2
  • 14
  • 3
    Primitive types aren't objects so they cannot be generic types. Your only choice is overloading the method for primitive types or simply living with this restriction. – OH GOD SPIDERS May 03 '23 at 08:36
  • You can also always convert your `int[]` to an `Integer[]` and then pass it to your method. – f1sh May 03 '23 at 08:38
  • As an aside: The fact that arrays are covariant and retained, while generics are invariant and erased makes it nasty to use generic arrays. Generic arrays should be avoided if possible. – Turing85 May 03 '23 at 08:44
  • @OHGODSPIDERS The strange thing. With Processing (Java Library) I can pass int[] arr and that's work fine. The question why that's work in this context ? And If that's work pretty sure there is a solution. – Knupel May 03 '23 at 09:39
  • @Krupel It was already explained why this doesn't work. A primitive type can simply never be a generic type. Have you taken an actual look at whatever method it is you say is having the behavior you want? Because if it is possible to pass an `int[]` into that method then that method is very likely defined as accepting `int[]` and not defined with generics. You say "I'm pretty sure there is a solution" and you already were told the solution: Overload the method. I'm almost certain that this is what has been done with the method you claim accepts both `Integer[]` and `int[]`. – OH GOD SPIDERS May 03 '23 at 09:44
  • @Krupel As an example of how it is done with method overloading you could take a look at existing base java classes like [java.util.Arrays](https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html). As you can see there most methods are defined once generic like `copyOf(T[] original, int newLength)` and then overloaded methods for each primitive type like `copyOf(float[] original, int newLength)` or `copyOf(double[] original, int newLength)`. – OH GOD SPIDERS May 03 '23 at 09:53
  • @OHGODSPIDERS why this code work in Processing, if it must don't work ? : ```void setup() { int [] arr = {0,1,2,3}; printArray(arr); printArray(reverse(arr)); } T [] reverse(T [] arr) { for(int i = 0 ; i < arr.length / 2; i++) { T buf = arr[i]; arr[i] = arr[arr.length - i - 1]; arr[arr.length - i - 1] = buf; } return arr; }``` – Knupel May 03 '23 at 10:12
  • Does this answer your question? [Can you use primitive types for generic parameters in a class or method?](https://stackoverflow.com/questions/21924365/can-you-use-primitive-types-for-generic-parameters-in-a-class-or-method) – user1803551 May 03 '23 at 11:03

1 Answers1

2

The problem is that you can not use T for int Primitive types.

What you can do is :

  • Create the int[] as you are doing, then convert it to Integer[] and then convert again to int[], which is not an efficient way
int[] arr = {0, 1, 2, 3};
Integer[] boxedArr = Arrays.stream(arr).boxed().toArray(Integer[]::new);
Integer[] reversedBoxedArr = reverse(boxedArr);
int[] reversedArr = Arrays.stream(reversedBoxedArr).mapToInt(Integer::intValue).toArray();
  • Use directly an Integer[]
Integer[] arr = {0, 1, 2, 3};
Integer[] reversedArr = reverse(arr);
Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148