S
Simone
Hello,
I need to write a method for printing an array of numbers as a string
(e.g. "(2,5,7)" ). The numbers can be int or double and I wish to
write only one method for both types so, remembering that Java wraps
primitive types into their corresponding objects (int in Integer,
double in Double) I wrote a method:
public String print( Number[] data ){ ... }
When I try to invoke the print method with an int[] or a double[]
argument, I get an error:
"The method print(Number[]) in the type MyClass is not applicable for
the arguments (int[])"
Actually, I expected Java would have wrapped the int[] into Integer[],
just as it wraps an int into an Integer. Since I can assign an
Integer[] to a Number[] I thought that by wrapping int into Integer,
Java would have allowed me to assign an int[] to a Number[]. Even if I
try to cast (i.e. trying to print( (Integer[])data ) ), Java says it
"Cannot cast from int[] to Integer[]".
Is there a way to fix this? Or I need to migrate all my int[] and
double[] in Integer[] and Double[]?
Many thanks in advance.
I need to write a method for printing an array of numbers as a string
(e.g. "(2,5,7)" ). The numbers can be int or double and I wish to
write only one method for both types so, remembering that Java wraps
primitive types into their corresponding objects (int in Integer,
double in Double) I wrote a method:
public String print( Number[] data ){ ... }
When I try to invoke the print method with an int[] or a double[]
argument, I get an error:
"The method print(Number[]) in the type MyClass is not applicable for
the arguments (int[])"
Actually, I expected Java would have wrapped the int[] into Integer[],
just as it wraps an int into an Integer. Since I can assign an
Integer[] to a Number[] I thought that by wrapping int into Integer,
Java would have allowed me to assign an int[] to a Number[]. Even if I
try to cast (i.e. trying to print( (Integer[])data ) ), Java says it
"Cannot cast from int[] to Integer[]".
Is there a way to fix this? Or I need to migrate all my int[] and
double[] in Integer[] and Double[]?
Many thanks in advance.