+ 5

Flexibility of array size

Can we dynamically increase our array size in Java?

19th Nov 2019, 12:34 PM
Raj Shekhar
Raj Shekhar - avatar
4 odpowiedzi
+ 5
Array are having fixed size but if you want an list with flexible size than there is a option. Like arrays only there is collections which are similar to array but not having fixed size. For example ArrayList, HashMap etc
19th Nov 2019, 12:58 PM
Nitin Tiwari
Nitin Tiwari - avatar
+ 4
Arrays are not dynamic that's why Linked lists come into picture. If you want to a larger array then just like Avinesh said copy the elements in larger array
19th Nov 2019, 1:46 PM
Arsenic
Arsenic - avatar
+ 2
Just like any other language if your entries go beyond the array size limit, you have to declare a new array with larger size and copy the elements of the previous array. But you can use an Array list in Java without doing the above. The size extension is internally handled.
19th Nov 2019, 1:01 PM
Avinesh
Avinesh - avatar
+ 1
No, increase/decrease of array is not possible once the size of array is fixed..Instead of array...you can use ArrayList which allows you to make changes in size.Based on the requirement,you can choose arrayList or LinkedList. For a better understanding, go through the link provided: https://www.sololearn.com/learn/Java/2179/
20th Nov 2019, 4:29 AM
Swati K
Swati K - avatar