Can size of array be changed in java programing language?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can size of array be changed in java programing language??

Hello coders..! I am facing difficult in a java code...please help me to design a code for manual memory allocation of array in java.

9th Feb 2019, 9:50 AM
Satyam Rai
Satyam Rai - avatar
4 Answers
+ 3
By definition an array require its size to be defined during creation. Otherwise, you're looking at an ArrayList with flexible capacity. 🔗 Additional info https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html
9th Feb 2019, 10:18 AM
Zephyr Koo
Zephyr Koo - avatar
+ 2
Just declare arrays as : int arr[] = {}; Don't give any arraysize in those sqaure brackets. This will help you to enter as many values you want in the array.
9th Feb 2019, 10:08 AM
Akshay Madrikar
Akshay Madrikar - avatar
+ 1
You cannot change the size of an array in Java. Its memory is fixed on allocation. There is no equvalent of C mrealloc What you can do is allocate a NEW arrary and copy the old data into it.
9th Feb 2019, 10:37 AM
RAHUL KUMAR
RAHUL KUMAR - avatar
+ 1
Your solution won't work Akshay Madrikar. You can no longer add an element to the array if you declare it as empty since arrays in Java are designed to have fixed length upon creation. https://code.sololearn.com/cCVwyW6sv8l5/?ref=app
9th Feb 2019, 11:58 AM
Lambda_Driver
Lambda_Driver - avatar