Hello, dear.

I wanted to pass any array to the method.

The problem arose with the transfer of an array of primitives, since the primitives are not derived from Object, respectively, and their arrays are the same.

Actually, the question is how to pass to the method an array of any primitives?

UPD: The variant with the transmission of multiple arguments ( public void someMethod(Object... args) ) does not suit me, since my argument type is processed through the Map<Class<?>, ISerializer> , therefore the sample is needed by the criterion of "array primitives. "

  • @ Yevgeny Karpov, give an example of an array in the form of a code, but it is not entirely clear how and what you are going to do. Everywhere, it seems, there are wrappers, autoboxing, etc., but you say all this is not right. So we need specifics. - Barmaley

3 answers 3

I suggest this solution: overload your method for all possible primitives as follows:

 public void someMethod(Object[] args) { //your code } public void someMethod(int[] ints) { Object[] args = new Object[ints.length]; for (int i=0; i<ints.length; i++) { args[i] = (Object) ints[i]; } return someMethod(args); } 

Now you can call your method, passing it an array of int, and write the implementation of the method for the array Object.

  • Yes, I did not guess ... the most it! Thank! - Yevgeny Karpov

Pass an array of wrappers over primitives ( Integer , Double , etc.), they were created for these purposes.

  • As I already wrote, I just need the opportunity to transfer a non-specific array of primitives ... I know perfectly well about wrappers, but in my particular case this is inconvenient. - Yevgeny Karpov
  • And you have nothing else to do. Generics do not work with primitives in any form, only with reference types. - fori1ton

Generally there is an abstract class Number. From which any Long, Double, Integer are inherited. And what a pity that there is no class of any "PrimitiveWrapper" :)

  • As I already wrote, I just need the opportunity to transfer a non-specific array of primitives ... I know perfectly well about wrappers, but in my particular case this is inconvenient. Besides, I can send wrappers as Object [], they work fine. - Yevgeny Karpov
  • ... and from which Character and Boolean , which are also wrappers over primitives, are NOT inherited. - fori1ton
  • `Boolean bool = new Boolean (true); System.out.println ("Boolean is instance of Object?" + (Bool instanceof Object)); Character cha = new Character ('x'); System.out.println ("Character is instance of Object?" + (Cha instanceof Object)); `Boolean is instance of Object? true Character is instance of Object? true What am I doing wrong? - Eugene Karpov
  • one
    Boolean bool = new Boolean (true); System.out.println ("Boolean is instance of Number?" + (Bool instanceof Number)); Character cha = new Character ('x'); System.out.println ("Character is instance of Number?" + (Cha instanceof Number)); Boolean is instance of Number? false Character is instance of Number? false Before you write something, bother to read carefully what you wrote before. The comment was related to the @argamidon answer, which suggested using a Number , but not taking into account the existence of such primitives as Boolean and Character . - fori1ton