Java Array class

Array Class in Java

Java provides the java.util.Arrays class to handle various array-related operations. This class offers methods for manipulating, searching, and sorting elements of an array. The Arrays class in Java serves as a utility class to work with arrays, making it easier to perform common operations.
Below are the commonly used methods of the Arrays class:

Sort Array

It is used to arrange array data in ascending order.

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] numbers = { 4, 2, 8, 1, 6 };
        Arrays.sort(numbers);
        for (int item : numbers) {
                System.out.print(item + ” “); // Output will be 1 2 4 6 8
           }
     }
}

ForEach

Java 8 introduced the forEach method, which can be used to perform a specified action on each element of the array.

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] numbers = { 10, 20, 30, 40, 50 };
        Arrays.stream(numbers).forEach(n -> System.out.println(n * 2));
        // Output: 20, 40, 60, 80, 100
    }
}

Copying Array

You can copy elements from one array to another array.

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] source = {40, 35, 70, 20, 55};
        int[] destination = Arrays.copyOfRange(source, 0, 4);
        for (int item : destination) {
            System.out.println(item); // Output will be 40, 35, 70, 20
        }
        // Note: The destination array has 4 elements, so it copies the first 4 elements from source.
    }
}

Equals Array

Check whether both array values and index are equal or not using a Boolean output.

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] firstArray = { 40, 20, 30, 45 };
        int[] secondArray = { 40, 30, 20, 45 };
        boolean areEqual = Arrays.equals(firstArray, secondArray);
        System.out.println(areEqual); // Output will be False as the index of the values is not the same.
    }
}

Convert Array to List

Arrays have a fixed size, which means once you define the size of an array, you cannot change it. However, lists (List<T>) are dynamic, meaning you can add or remove elements as needed without worrying about size limitations.

import java.util.Arrays;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        // Define an array
        Integer[] numbersArray = { 10, 20, 30, 40, 50 };
        // Convert the array to a list
        List<Integer> numbersList = Arrays.asList(numbersArray);
        // Convert to mutable list
        List<Integer> mutableList = new ArrayList<>(numbersList);
        // Add an element to the list
        mutableList.add(25);
        // Remove an element from the list
        mutableList.remove(Integer.valueOf(20));
        // Print the list elements
        System.out.println(“List elements:”);
        for (int number : mutableList) {
                System.out.println(number);
        }
    }
}

Output:

List elements:
10
30
40
50
25

Example: Using an Array with an If-Else Statement

Here is a simple example of using an array with an if-else statement in Java. In this example, we create an array of numbers and check if each number is even or odd using an if-else statement.

public class Main {
    public static void main(String[] args) {
        int[] numbers = { 10, 5, 8, 3, 12 };
        for (int num : numbers) {
            if (num % 2 == 0) {
                System.out.println(num + ” is even.”);
            } else {
                System.out.println(num + ” is odd.”);
            }
        }
    }
}

Explanation:

public class Main {
    public static void main(String[] args) {
        int[] numbers = { 10, 5, 8, 3, 12 };
        // Define an array of integers with specific values
        for (int num : numbers)
            // Iterate through each element in the numbers array
        {
            if (num % 2 == 0)
               // Check if the current number is even (remainder of division by 2 is 0)
            {
                System.out.println(num + ” is even.”);
                // Print that the number is even using string concatenation
            } else
            // If the number is not even
            {
                System.out.println(num + ” is odd.”);
                // Print that the number is odd using string concatenation
            }
        }
    }
}

Tasks:
1. Sort an Array: Create an integer array with the values {3, 1, 4, 1, 5, 9}. Use Arrays.sort to sort the array and print the sorted values.
2. Double Each Element: Create an integer array with the values {1, 2, 3, 4, 5}. Use a loop to double each element and print the modified array.
3. Copy Array: Create a source integer array with the values {11, 22, 33, 44, 55}. Create a destination array of size 3. Use System.arraycopy to copy the first 3 elements from the source to the destination and print the destination array.
4. Check Equality: Create two integer arrays, one with the values {5, 10, 15} and the other with the values {5, 10, 15}. Use Arrays.equals to check if the arrays are equal and print the result.
5. Even or Odd Check: Create an integer array with the values {7, 14, 21, 28, 35}. Use a loop and an if-else statement to check if each number is even or odd, and print the result.
6. Sort and Print Strings: Create a string array with the values {“banana”, “apple”, “cherry”}. Use Arrays.sort to sort the array alphabetically and print the sorted values.
7. Find and Print Even Numbers: Create an integer array with the values {12, 15, 18, 21, 24}. Use a loop and an if-else statement to print only the even numbers from the array.

Course Video

YouTube Reference :

Frequently Asked Questions

Still have a question?

Let's talk

A free course covering fundamental Java programming concepts for beginners.

Anyone new to programming or looking to learn Java basics.

Yes, the course is completely free to access and learn.

No, all course materials are provided for free.

Java syntax, data types, control structures, OOP concepts, and exception handling.

Building web apps, mobile apps, desktop software, and enterprise systems.

It’s platform-independent, object-oriented, secure, and supports multithreading.

Iqra Technology aims to make Java learning accessible to everyone at no cost.

No prior programming knowledge is required.