You are currently viewing Java Arrays Class

Java Arrays Class

  • Post author:
  • Post category:Java
  • Post comments:0 Comments
  • Post last modified:March 31, 2024

In Java, the java.util.Arrays class is a utility class that provides various static methods to work with arrays. It includes methods for sorting, searching, copying, filling, and converting arrays. The Arrays class is part of the Java Collections Framework (java.util package) and is widely used when working with arrays in Java applications.

Table of Contents

  1. Introduction to Arrays Class
  2. Common Methods in Arrays Class
  1. Example Usage
  1. Conclusion

1. Introduction to Arrays Class

The java.util.Arrays class provides static methods for manipulating arrays. It does not have a public constructor because all of its methods are static. Some of the common operations that the Arrays class provides include:

  • Sorting arrays
  • Searching arrays
  • Filling arrays with a specific value
  • Copying arrays
  • Converting arrays to a string representation

These methods simplify common tasks related to arrays and provide efficient implementations for these operations.

2. Common Methods in Arrays Class

Sorting Arrays

  • void sort(int[] arr): Sorts the specified array of int into ascending numerical order.
  • void sort(T[] arr): Sorts the specified array of objects into ascending order.
  • void sort(T[] arr, Comparator<? super T> c): Sorts the specified array of objects according to the order induced by the specified comparator.

Searching Arrays

  • int binarySearch(int[] arr, int key): Searches the specified array of int for the specified value using the binary search algorithm.
  • int binarySearch(T[] arr, T key): Searches the specified array of objects for the specified object using the binary search algorithm.

Filling Arrays

  • void fill(int[] arr, int value): Assigns the specified int value to each element of the specified array of int.
  • void fill(T[] arr, T value): Assigns the specified object reference to each element of the specified array of objects.

Copying Arrays

  • void copyOf(int[] original, int newLength): Copies the specified array, truncating or padding with zeros (if necessary) so the copy has the specified length.
  • void copyOf(T[] original, int newLength): Copies the specified array, truncating or padding with nulls (if necessary) so the copy has the specified length.

Converting Arrays

  • String toString(int[] arr): Returns a string representation of the contents of the specified int array.
  • String toString(T[] arr): Returns a string representation of the contents of the specified array of objects.

3. Example Usage

Let’s see some examples of how to use these methods in the Arrays class.

3.1. Sorting an Array

import java.util.Arrays;

public class ArraysExample {
    public static void main(String[] args) {
        int[] numbers = {5, 2, 8, 1, 9};

        // Sort the array
        Arrays.sort(numbers);

        // Print the sorted array
        System.out.println("Sorted Array: " + Arrays.toString(numbers));
    }
}

Output:

Sorted Array: [1, 2, 5, 8, 9]

3.2. Searching in an Array

import java.util.Arrays;

public class ArraysExample {
    public static void main(String[] args) {
        int[] numbers = {1, 3, 5, 7, 9};

        // Search for the number 5
        int index = Arrays.binarySearch(numbers, 5);

        // Print the index
        System.out.println("Index of 5: " + index);
    }
}

Output:

Index of 5: 2

3.3. Filling an Array

import java.util.Arrays;

public class ArraysExample {
    public static void main(String[] args) {
        int[] numbers = new int[5];

        // Fill the array with value 10
        Arrays.fill(numbers, 10);

        // Print the filled array
        System.out.println("Filled Array: " + Arrays.toString(numbers));
    }
}

Output:

Filled Array: [10, 10, 10, 10, 10]

3.4. Copying Arrays

import java.util.Arrays;

public class ArraysExample {
    public static void main(String[] args) {
        int[] source = {1, 2, 3};

        // Copy the array
        int[] destination = Arrays.copyOf(source, source.length);

        // Print the copied array
        System.out.println("Copied Array: " + Arrays.toString(destination));
    }
}

Output:

Copied Array: [1, 2, 3]

3.5. Converting Arrays

import java.util.Arrays;

public class ArraysExample {
    public static void main(String[] args) {
        int[] numbers = {1, 2, 3, 4, 5};

        // Convert array to string
        String arrayString = Arrays.toString(numbers);

        // Print the array string
        System.out.println("Array as String: " + arrayString);
    }
}

Output:

Array as String: [1, 2, 3, 4, 5]

4. Conclusion

The Arrays class in Java provides a wide range of utility methods for working with arrays. It simplifies common tasks such as sorting, searching, filling, copying, and converting arrays. By using the methods provided by the Arrays class, you can perform these operations efficiently and with less boilerplate code.

In this tutorial, we covered some of the most commonly used methods in the Arrays class, along with examples of how to use them. These methods are essential

for manipulating arrays in Java applications.

Additional Resources:

Leave a Reply