+ 7
Can i add different data_types of element in java array.
e.g int[] x=new int[4] can i add two int and two float in it but it give error,but how i can do it.
4 Respostas
+ 7
Typically Java arrays are made up from values that all have the same data-type.
+ 3
do you mean have one element as a float and one as an int... I don't think this is possible... they should be of the same data type
+ 2
You cannot add different types of data to a java array. Java is strongly typed language. But if you want to add different types, you have to use an ArrayList object.
ArrayList li = new ArrayList();
li.add(10);
li.add(30.45);
System.out.println(li);
For this to work, you need to import ArrayList at the top of your code.
import java.util.ArrayList;
0
Hi