Press "Enter" to skip to content

How to Create and Perform Operations on Arrays in Java

Arrays provide an easy way for developers to store and retrieve data when creating applications.

An array is a data structure that’s used to store, retrieve, and manipulate a collection of elements that share the same data type. Though arrays often store a vast list of elements, the entire array can be accessed using a single identifier—this is known as the array name.

However, if the goal is only to access a single element in an array, then the index of the required element has to be combined with the array name for access to be granted.

How to Use an Array

Arrays can be used in almost every programming language. Declaring and defining array elements are the two basic requirements that need to be met before you can start using any array.

Declaring an Array in Java

In Java, arrays can be declared in one of two ways; the major difference between each method is that one takes up significantly more space than the other when it comes time to define its variables.

Declaring an Array Example


public class Arrays {
public static void main(String[] args) {
//declaring an integer array
int[] arr1 = new int[10];
}
}

With the code above, an array has been declared with the name arr1. This array can store a list of 10 integers.

If the 10 integers that should be stored in arr1 above are readily available for use, this process will require an additional 10 lines of code.

Populating an Array Example


public class Arrays {
public static void main(String[] args) {
//declaring an integer array
int[] arr1 = new int[10];
//populate the array with 10 integer elements
arr1[0] = 2;
arr1[1] = 4;
arr1[2] = 6;
arr1[3] = 8;
arr1[4] = 10;
arr1[5] = 12;
arr1[6] = 14;
arr1[7] = 16;
arr1[8] = 18;
arr1[9] = 20;
}
}

In arrays, the term “index” refers to the position of a specific element in a list. Each index position is identified by a numeric value and by default every array starts at position zero.

Therefore, if you want to place a list of even numbers into an array, the first element has to be placed at index position zero. This is represented by the name of the array followed by a pair of square brackets that encloses the number zero.

An element is placed in a particular position in an array using the equal sign, as you can see in the example above. The example also shows that all 10 positions created when the arr1 array is declared are now assigned an even integer value from 2-20.

The array is now fully populated. If for some reason you decide to delete the final line of code in the example above, the array will still be fully populated. The only difference would be that the element at index position 9 would now be zero; this is because every position in an integer array that’s created without an element is assigned a value of zero by default.

That zero value is only overridden when another value is explicitly assigned to the index position, as is the case in the example above.

Declaring and Populating an Array

There’s a much simpler way to accomplish the same thing with only one line of code. Instead of declaring an array, then assigning each element to a position one at a time, you can simply declare an array and assign the appropriate elements to it all in one go.

Declaring and Assigning Variables to an Array Example


public class Arrays {
public static void main(String[] args) {
//declare and populate the array with 10 integer elements
int[] arr2 = {1,3,5,7,9,11,13,15,17,19};
}
}

In the example above, arr2 is created and populated with a list of 10 odd integer numbers. Like the previous array, arr2 is now complete and ready for use.

Accessing Variables in an Array

In any programming language, gaining access to an array after it’s been created and populated is very easy if you know what position that element occupies. Going back to our example above, say you wanted the value 11 to perform some operation in your program.

To gain access to this value, you’d need to know two things; the name of the array that the value is a part of, and the position of the value in that array. From our example, you can see that the value 11 is in an array called arr2 and at the position of index 5.

The following code will provide you with access to that value.


public class Arrays {
public static void main(String[] args) {
//declare and populate the array with 10 integer elements
int[] arr2 = {1,3,5,7,9,11,13,15,17,19};
//printing the value at position 5 to the console
System.out.println(arr2[5]);
}
}

The code above returns the value 11. Depending on what you wish to do with the element, you can either assign it to a new variable or print it to the console. In the example above, the value is printed to the console, which produces the following result on the screen.


11

This approach isn’t very practical if you’re dealing with an array that has thousands of elements and need to retrieve hundreds of different elements at different points. This is why for loops are typically used to gain access to multiple array variables at a time.

Using For loops With Your Arrays

for loop is one of three iterative structures used to achieve repetition in programming. Even from a naïve perspective, if you consider the process of accessing multiple elements in an array, you’ll find that a lot of repetition will need to take place.

Printing all the elements in the odd number array above to a console might seem like a very daunting task if you had to explicitly retrieve and print one element at a time before moving to the next.

The for loop does this exact thing implicitly with less than half of the code that it would take to do it explicitly.

Retrieving Array Elements With a For Loop Example


public class Arrays {
public static void main(String[] args) {
//declaring and initializing the array
int[] arr2 = {1,3,5,7,9,11,13,15,17,19};
//declaring the count variable
int count;
//using the for loop to print each element in the array to the console
for(count = 0; count < arr2.length; count++) {
System.out.println(arr2[count]);
}
}
}

In the example above, the integer variable named count takes on the index position of each element in the array at different points in time. This is achieved in the first line of the for loop, where count is initialized to zero then restricted to values that are less than the length of the array (so less than 10).

The final operation run on the count value is to increment it by one each time the for loop executes, before passing its value to the index position of the array. The second line of the for loop produces the following result in the console.


1
3
5
7
9
11
13
15
17
19

Arrays Make Storing and Retrieving Data Easier

From this article, you’ve gained the skills required to efficiently store and retrieve data from arrays in Java. You now know how to use for loops on your Java arrays, and understand just how well organized this function is.

The Java language is also well structured into sections known as classes, and if you want to use the language  effectively you’ll need to know how to create classes in it.