I have an integer array int[] a = new int [5]
.
In my code I am storing only 2 values at indices 0 and 1.
a[0]=100
and a[1]=101
Now I need to get the array size/length as 2.
What I should do?
You array length is 5, not 2. You've defined your array to be 5 elements long, how many you ended up using is irrelevant.
What can you do instead is this:
List<Integer> a = new ArrayList<Integer>();
a.add(100);
a.add(101);
System.out.println(a.size());
will give you 2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With