Many a times you want to operate on individual elements of an array. For example, you may want to apply some operators on some, but not all elements of the array. We have already seen how declare and initialize Java arrays. In this lesson we learn how to operate on array elements.
You can initialize, modify, access and delete individual elements. In the next section we will look at code that demonstrates each of these operations.
Note that removal of an element has limited scope because length of an array is always fixed. Depending on the problem scenario, a delete operation may just put a null or an empty object at the location to be deleted.
A Java array knows its size and exposes it using the length property. Since every java array knows its size, segmentation fault cannot occur. A segmentation fault occurs when there is an illegal access to memory. Java’s security features prevent access of memory beyond the array limits.
Example of Array Element Manipulation
Let’s look at the code below which accesses, modifies and deletes (which is nothing but a modify operation) individual elements of array –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | package com.codingraptor; public class Arrays2 { public static void main(String[] args) { // initialize the elements of array // no need to declare the length explicitly String[] website = { "Coding", // website[0] "Raptor", // website[1] ".com ", // website[2] "is ", // website[3] "great", // website[4] "." // website[5] }; // the following statement accesses and prints all array elements // ordinarily, you need to use looping construct // this is for demonstration only System.out.println(website[0] + website[1] + website[2] + website[3] + website[4] + website[5]); // the following code modifies element website[4] = "awesome"; // let's check out the modifications System.out.println(website[0] + website[1] + website[2] + website[3] + website[4] + website[5]); // remove an element from an array has limited scope because // arrays' length is fixed // most of the time putting a null/empty object at the position to be removed // should suffice website[5] = ""; // print to verify System.out.println(website[0] + website[1] + website[2] + website[3] + website[4] + website[5]); } } |
The above program when executed, gives the following output
1 2 3 | CodingRaptor.com is great. CodingRaptor.com is awesome. CodingRaptor.com is awesome |
Manipulating Elements of Java Array via Copy of Reference
It is possible to manipulate individual elements of an array via the array name or through a copy of the reference to the array. Consider the code below
1 2 3 4 5 | int[] arr = {210, 500}; int[] arr2; arr2 = arr; |
Note that the following statement does not reserve any memory. It is merely a declaration
1 | int[] arr2; |
And the following statement just creates a new name for the memory locations created earlier.
1 | arr2 = arr; |
Any changes to the array made through arr2 variable is visible via arr variable and vice versa
1 2 3 4 5 6 7 8 9 10 11 12 13 | arr[0] *= 2; // stores 210 * 2 in arr[0] // the above change is visible through // arr2 as well. // Because both arr and arr2 are referencing // the same memory location System.out.println(arr2[0]); // prints 420 |
As you have seen in the example above accessing and manipulating individual element is possible but is very cumbersome. Therefore, in the next and the fifth on arrays we discuss iteration mechanisms.