+ 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.

24th Jun 2018, 4:54 PM
Maninder $ingh
Maninder $ingh - avatar
4 Antworten
+ 7
Typically Java arrays are made up from values that all have the same data-type.
24th Jun 2018, 5:19 PM
Scott
+ 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
24th Jun 2018, 5:16 PM
laura
+ 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;
25th Jun 2018, 6:53 AM
Sachintha Sampath
Sachintha Sampath - avatar
0
Hi
25th Jun 2018, 9:35 AM
[No Name]