+ 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.
5 Respostas
+ 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.
+ 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
0
In that case you should use a List
0
yes like
int numbers[]={1,2,3,4,5,6,7};
0
yes ofcourse...you can just use m,n or anything ...you dont have to define its length anyway