1. Introduction
In this lesson of Java course we will learn about Java Arrays. One of the key features of Java is its built-in support of arrays, which are used to store collections of data. In this article, we will cover the basics of arrays in Java, including how to set up an array, access its elements, and perform common operations.
Table of Contents
2. Java Arrays Definition
An array is a data structure that stores a fixed-size collection of elements of the same type. In Java, arrays are objects and they are created dynamically at runtime. The size of an array is fixed and cannot be changed once it is created.
An array is a data structure in Java that stores a fixed number of values of the same type. These values are indexed, so they can be accessed using numbers (also known as indices).
A Java arrays can be thought of as a numbered list of cells, with each cell containing a variable that holds a value. In Java, the numbering for arrays begins at 0. Arrays can be of primitive data types such as int, float, and boolean, as well as of object data types such as String, Object, and custom types.
2.1. Setting up an Array
Now that we clearly understand what arrays are, let’s explore their uses in depth. This includes learning the basics, such as how to declare and initialize an array, as well as more advanced concepts like sorting and searching. First, we’ll start with the basics of declaring and initializing an array.
2.2. Declaration of Java Arrays
To declare an array, we need to specify the type of the elements it will store and the name of the array followed by square brackets. For example, to declare an array of integers, we can use the following syntax:
int[] numbers;
//or
int numbers[];
2.3. Initialization Arrays in Java
Now that we have a solid understanding of arrays, let’s delve into the various ways to initialize them. While there are multiple methods for initializing an array, we will discuss the most common ones in this article.Once an array is declared, we can initialize it by assigning it a new array object.
We can create a Java array by specifying its size and the initial values of its elements. For example, the following code creates an array of integers with size 5 and assigns the values 1, 2, 3, 4, and 5 to its elements:
class JavaArrysExample {
public static void main(String[] args) {
int[] anArray = new int[10];
//OR
int[] numbers = new int[] {1, 2, 3, 4, 5};
}
}
3. Accessing Array Elements
Once an array is created and initialized, we can access its elements by using the array name followed by the index of the element in square brackets. The index of an element in an array starts at 0
, so the first element has an index of 0
, the second element has an index of 1
, and so on. For example, the following code prints the value of the first element in the array:
class JavaArrysExample {
public static void main(String[] args) {
int[] numbers = new int[] {1, 2, 3, 4, 5};
System.out.println(numbers[0]); // prints 1
}
}
We can also change the value of an element by using the assignment operator (=
) and specifying the new value.
numbers[3] = 6;
4. Looping Over Java Arrays
Java provides several ways to loop over an array and access its elements. The most common method is to use a for loop. The following code uses a for loop to print the values of all elements in the array:
class JavaArrysExample {
public static void main(String[] args) {
int[] numbers = new int[] {1,2,3,4,5};
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
}
}
The above code will output the numbers 1 to 5 to the console. We made use of the “length” property, which is a public property that returns the size of the array. It is possible to use other loop mechanisms, such as while or do-while, but just like with other Java collections, arrays can also be iterated over using the enhanced for-loop, also known as the for-each loop.
Another way to loop through an array is by using the for-each loop. This loop allows us to iterate through the array without the need to know the size or the index of the elements. Here is an example for same:
class JavaArrysExample {
public static void main(String[] args) {
int[] numbers = new int[] {1, 2, 3, 4, 5};
for (int number : numbers) {
System.out.println(number);
}
}
}
This example produces the same output as the previous one, but eliminates the need for index boilerplate code. The enhanced for-loop or for-each loop is an option when:
- We don’t need to change the array. Using this loop to update an element in the array will not affect the actual element in the array.
- We don’t need the indices for any other purpose
Summary
Java Arrays are an important feature of Java and are widely used to store collections of data. In this article, we have covered the basics of arrays in Java, including how to set up an array, access its elements, and perform common operations, such as looping, sorting, and converting to a list. Understanding the basics of arrays in Java is essential for developing efficient and effective code.
As Always, the source code for this course is available on the GitHub repository.