Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic Array List size

How can I set size of a dynamic array with Java?

I tried setsize(...) with the array variable but not working. How can I do this?

like image 462
Yuzakii Avatar asked Dec 05 '22 16:12

Yuzakii


1 Answers

array size needs to be fixed while initialization, use List instead and then have array from List using toArray()

For example:

List<Integer> listOfInt = new ArrayList<Integer>(); //no fixed size mentioned
listOfInt .add(1);
listOfInt .add(2);
listOfInt .add(3);
//now convert it to array 
Integer[] arrayOfInt = list.toArray(new Integer[listOfInt .size()]);
like image 198
jmj Avatar answered Dec 15 '22 23:12

jmj