Is it possible to not declare the length of an array? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Is it possible to not declare the length of an array?

it would be useful when you don't know how many data you have to store.

23rd Jun 2016, 1:14 PM
Tiziano Miccoli (Titus75)
Tiziano Miccoli (Titus75) - avatar
5 Answers
+ 1
list is another data structure, little more complex, you will enjoy. list has no leght predefined. and it doesnt alocate memory before be used.
29th Jun 2016, 5:43 PM
Ronaldo Marques
Ronaldo Marques - avatar
+ 1
Arrays have a fixed size and can not store items without the size , even if you have not added any items , but specify the size , then the items will be inserted by default. Example: int[] a = new int[3]; System.out.println(a[2]); Output: 0 You need a dynamic array , it resizes automatically when adding new elements . It is possible to implement the List interface . Example: List<Integer> listNumbers = new ArrayList<>(); listNumbers.add(12); System.out.println(listNumbers.get(0)); //System.out.println(listNumbers.get(1)); //IndexOutOfBoundsException listNumbers.add(45); System.out.println(listNumbers.get(1)); //now its Work
27th Jul 2016, 1:46 PM
Shwarz Andrei
0
In that case you should use a List
28th Jun 2016, 6:33 PM
Álvaro Cid Rodríguez
0
yes like int numbers[]={1,2,3,4,5,6,7};
1st Jul 2016, 8:48 PM
Abdur-Rahmaan Janhangeer
Abdur-Rahmaan Janhangeer - avatar
0
yes ofcourse...you can just use m,n or anything ...you dont have to define its length anyway
21st Jul 2016, 2:05 PM
Harshil Soni
Harshil Soni - avatar