Summary: In this tutorial, you will learn to create, initialize, access and modify different types of arrays in Java.

Array in Java

In Java, an array is a collection of elements that are stored in a contiguous memory location. Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.

For example:

int[] numbers = {1, 2, 3, 4, 5};

How to Declare or Create Array in Java?

To declare an array in Java, you need to specify the type of the elements that the array will hold, followed by the name of the array, and then square brackets. For example:

int[] numbers;

This creates an array of integers called “numbers”, but it does not allocate any memory for the array yet. To create an array and allocate memory for it at the same time, you can use the “new” operator, followed by the size of the array in square brackets. For example:

int[] numbers = new int[5];

This creates an array of integers called “numbers” with a size of 5. The array is automatically initialized with the default value for the element type (0 for int).

Similarly, you can create array for other primitive or reference types as well:

double[] numbers = new double[5];
String[] names = new String[10];

You can also create an array by specifying elements (comma separated) inside a curly brace. For example:

int[] numbers = {1, 2, 3, 4, 5};

How to Access elements in Array?

To access an element of an array, you can use the indexing operator ([]). The index must be an integer value between 0 and the size of the array minus 1.

For example:

int firstElement = numbers[0];
int lastElement = numbers[4];

You can also use a loop to iterate over the elements of an array. Here is an example using a for loop:

for (int i = 0; i < numbers.length; i++) {
  System.out.println(numbers[i]);
}

You can also use the enhanced for loop, which is a simpler and more readable way to iterate over the elements of an array:

for (int number : numbers) {
  System.out.println(number);
}

Similarly, you can use the indexing operator to modify the value of an element of the array:

numbers[0] = 100;

Types of Arrays in Java

In Java, there are several types of arrays that you can use, depending on your needs:

1. Single-dimensional arrays

These are the most basic type of arrays, and they consist of a single list of elements. For example:

int[] numbers = {1, 2, 3, 4, 5};

2. Multidimensional arrays

These are arrays that contain one or more arrays as elements. There are two types of multidimensional arrays:

Two-dimensional arrays

These are arrays that contain a list of rows and columns. For example:

int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

You will study more about this in later post.

Jagged arrays

These are arrays that have a different number of elements in each row. For example:

int[][] jagged = {{1, 2, 3}, {4, 5}, {6, 7, 8, 9}};

3. Primitive arrays

These are arrays that hold values of primitive types (such as int, char, etc.). For example:

int[] numbers = {1, 2, 3, 4, 5};
char[] letters = {'a', 'b', 'c'};

4. Reference arrays

These are arrays that hold references to objects. For example:

String[] names = {"John", "Paul", "George", "Ringo"};

5. Array objects

These are objects that are created from the “java.util.Arrays” class, and they provide additional methods for manipulating arrays. For example:

int[] numbers = {1, 2, 3, 4, 5};
Arrays.sort(numbers);

In summary, single-dimensional arrays are the most basic type of arrays, and they are useful when you need to store a list of elements of the same type. Multidimensional arrays are useful when you need to store data in a grid-like structure.

Primitive arrays are useful when you need to store values of primitive types, and reference arrays are useful when you need to store references to objects. Finally, array objects are useful when you need to perform operations on arrays, such as sorting or searching.

Example using Array in Java

Here is an example of how to use an array in Java to store and manipulate a list of integers:

import java.util.Arrays;

public class ArrayExample {
  public static void main(String[] args) {
    // Declare and create an array of integers
    int[] numbers = {1, 2, 3, 4, 5};

    // Print the array
    System.out.println(Arrays.toString(numbers));

    // Access and modify an element of the array
    numbers[0] = 100;
    System.out.println(numbers[0]);

    // Find the length of the array
    int arrayLength = numbers.length;
    System.out.println("Array length: " + arrayLength);

    // Iterate over the elements of the array
    for (int number : numbers) {
      System.out.println(number);
    }
  }
}

This program declares and creates an array of integers called “numbers”, and then it uses several methods to manipulate the array:

  • The “Arrays.toString()” method is used to print the array.
  • The indexing operator ([]) is used to access and modify an element of the array.
  • The “length” property is used to find the length of the array.
  • The enhanced for loop is used to iterate over the elements of the array.

The output of the program is:

[100, 2, 3, 4, 5]
100
Array length: 5
100
2
3
4
5

Leave a Reply