Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java: how can I create a dynamic array without forcing the type of the values?

Tags:

java

arrays

I need to create a dynamic array in Java, but the values type differ from String to Int to float. how can I create a dynamic list that I don't need to give it in advanced the type of the values?

The keys just need to be ascending numbers (1,2,3,4 or 0,1,2,3,4)

I checked ArrayList but it seems that I have to give it a fixed type for the values.

thanks!

like image 869
ufk Avatar asked Apr 09 '10 13:04

ufk


2 Answers

You can have an array or an ArrayList of Objects which will allow you to contain String, int, and float elements.

like image 138
Anthony Forloney Avatar answered Sep 20 '22 13:09

Anthony Forloney


You can use this:

List<Object> myList = new ArrayList<Object>();

Integer i = 1;
Double d = 1.2;
String s = "Hello World";

myList.add(i);
myList.add(d);
myList.add(s);
like image 22
Daniel Rikowski Avatar answered Sep 21 '22 13:09

Daniel Rikowski