Accessing Array Elements In JAVA
Now the thing is that we have been accessing the individual elements from the array, but we are doing it manually. Is there something that is better than this? Well, we can make use of for loop to iterate through the individual elements in the array. There is one more thing that we are going to discuss, which is called an enhanced for loop, but first let’s get started with the traditional for loop. So, the same program which we did above but manually, let’s put a for loop there.
public class IntegerArray {
public static void main(String[] args) {
int[] myIntArray = new int[5];
for (int i = 0; i < 5; i++) {
myIntArray[i] = i*5; // this is adding some data to the array…
}
for (int i = 0; i < 5; i++) {
159
System.out.println(myIntArray[i]);
}
}
}
Well, in the above program, we did the same thing, but this time, we used the for loop, and also we could print all the elements using another for a loop.
Now if you might be wondering why did I run the for loop just 5 times? Like from 0 to 4? well, this is because the array index starts from zero, and we have declared an array for 5 elements, so the index of the first element would be 0, and for the last element would be 4. So, we are trying to access the array elements from index 0 to 4, and the length of the array(the number of elements in the array) is 5. Well, you will surely understand this soon.
Even if we now try to access myIntArray[5] anyways, we will get an ArrayIndexOutOfBoundsException, which means that no such index is present for that particular array. You can try running the same loop for i<=5, and it will throw the exception, since no such index 5 is present, since the final (last) index as we know is 4.
Now let’s talk about something called the length of the array.
Well, the length of the array simply means the number of elements in the array. The array we created in the previous program had a length of 5. So, it contained elements from index 0 to 4. So, from this we can understand that the final index is going to be one less than length, that is length-1.
We have a field called length, which stores the length of the array. So, we can directly use it to get the length of the array once the array is created, instead of hardcoding the values every time.
Notice that every time we are running the loop in the above program, we are hardcoding the length 5 of the array. What if we create an array of lengths 10 now? Wherever we have used the length in the above program, we have to change it everywhere.
But if we use the length field, we need not change it everywhere, but just once, while we are creating an array since the length field holds the length of the array.
So, now let’s have an example program, where we use the length field instead of hardcoding the length of the array everywhere.
Notice that at the time of the creation of the array, the length is to be given. After that, we do not have to hardcode the value everywhere, instead, we are going to use the length variable.
Have a look at the program –
public class IntegerArray {
public static void main(String[] args) {
int[] myIntArray = new int[5];
System.out.println(“Length of the array : ” + myIntArray.length);
for (int i = 0; i < myIntArray.length; i++) {
myIntArray[i] = i*5;
}
for (int i = 0; i < myIntArray.length; i++) {
System.out.print(myIntArray[i] + ” “);
160
}
}
}
So, in the above program, you can see that we can comfortably use the length field with the array to get the length of the array. Note that the length of the array once set, cannot be changed, since it is a final variable. Also in the loops, we could comfortably use the length variable. So, if we decide to change the length of the array, we do not need to change it everywhere, except at the time of creation.