Array

Java Arrays

In Java, an array is a data structure that allows for the storage of a fixed-size collection of elements with the same data type. It provides a way to group related data items under a single variable name. Each element in an array is accessed by its index, which is an integer value that ranges from 0 to (array length – 1). Random access is the process of accessing elements directly by their index.

Declaring an array in Java:

To declare an array in Java, you specify the data type of the elements it will hold, followed by the square brackets “[]”, and then the array variable name. For example:

dataType[] arrayName;

Here, dataType represents the data type of the elements, and arrayName is the name of the array variable. For instance, if you want to create an array of integers, you would declare it like this:

Creating an array:

After declaring the array, you need to allocate memory for it using the new keyword. The new keyword creates an instance of the array on the heap, and its size is determined by specifying the number of elements it can hold inside square brackets “[]”. For example, to create an array of integers that can store 5 elements, you would do:

int[] numbers = new int[5];

Initializing an array:

You can also initialize the array with specific values at the time of creation. For example:

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

Accessing elements in the array:

You can access individual elements in the array using their index. The index starts from 0, so to access the first element, you would use index 0, and to access the second element, you would use index 1, and so on.

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

// Accessing elements by index

int firstElement = numbers[0]; // 1

int secondElement = numbers[1];// 2

 

Array length:

The amount of elements the array may hold is determined by its length.You can get the length of an array using the length property:

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

int arrayLength = numbers.length;// 5

Arrays are a fundamental part of Java and are used extensively in programming to store and manipulate collections of data efficiently. They are particularly useful when you need to work with a fixed number of related values of the same type.

Looping Through Array Elements

We may also loop over each element of the array with Java. For example,

Example: Using For Loop

public class Main {

    public static void main(String[] args) {

        // create an array
        int[] nums = { 12, 4, 5 };

        // loop through the array
        // using for loop
        System.out.println("Using for Loop:");
        for (int i = 0; i < nums.length; i++) {
            System.out.println(i);
        }
    }
}

Output:

Using for Loop:

0

1

2

In the above example, we are using the for Loop in Java to iterate through each element of the array. Notice the expression inside the loop,

age.length

In this case, the array’s length property is being used to determine the array’s size.

Additionally, we can traverse through an array’s elements using the for-each loop.

Example: Using the for-each Loop

public class Main {

    public static void main(String[] args) {

        // create an array
        int[] age = { 1, 4, 5 };

        // loop through the array
        // using for loop
        System.out.println("Using for-each Loop:");
        for (int a : age) {
            System.out.println(a);
        }
    }
}

Output:

Using for-each Loop:

1

4

5

Advantages

  • Code Optimization: Utilizing arrays allows for optimized code, enabling efficient retrieval and sorting of data.
  • Random access: Any data at an index location is available to us.

Disadvantages

  • Size Limit: Arrays have a fixed size, meaning they can store only a predetermined number of elements. Unlike collections in Java that can grow dynamically, arrays cannot change their size during runtime.
Visited 1 times, 1 visit(s) today

Comments are closed.

Close