Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the length/size of an integer array

Tags:

java

arrays

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?

like image 771
athresh Avatar asked Dec 02 '22 03:12

athresh


1 Answers

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

like image 180
iluxa Avatar answered Dec 25 '22 12:12

iluxa