int[] arrayInt = new int[] {1, 2, 3, 4, 5, 6};
O/P is: -
arrInt.length = 6
array.length is actually a public final member/field of the array, which contains the number of components of the array. It is final because arrays in java are immutable, which means once the array is defined then an array can not be grow or shrink, however the elements within the array can change. So, it means once the size of array is defined it can not be changed, that is why it is final and treated as a property/field/instance variable.
A java array is allocated with a fixed number of element slots. The "length" attribute will tell you how many. That number is immutable for the life of the array. For a resizable equivalent, you need one of the java.util.List classes - where you can use the size() method to find out how many elements are in use.
List<Integer> list = new ArrayList<Integer>(20);
list.add(1);
list.add(2);
list.add(3);
O/P is: -
list.size() = 3
No comments:
Post a Comment