Java Tutorial

Operators

Flow Control

String

Number and Date

Built-in Classes

Array

Class and Object

Inheritance and Polymorphism

Exception Handling

Collections, Generics and Enumerations

Reflection

Input/Output Stream

Annotation

Java Arrays Utility Class

The java.util.Arrays class is a utility class in Java that provides several methods for manipulating arrays, such as sorting, searching, filling, and comparing. In this tutorial, we will go through some common methods provided by the Arrays class.

  • Import the Arrays class:

To use the Arrays class, you first need to import it:

import java.util.Arrays;
  • Sort an array:

The Arrays.sort() method sorts an array in ascending order.

int[] intArray = {5, 3, 1, 4, 2};
Arrays.sort(intArray); // intArray becomes {1, 2, 3, 4, 5}
  • Binary search in a sorted array:

The Arrays.binarySearch() method performs binary search on a sorted array and returns the index of the searched element. If the element is not found, it returns a negative value.

int[] intArray = {1, 2, 3, 4, 5};
int index = Arrays.binarySearch(intArray, 4); // index = 3
int notFound = Arrays.binarySearch(intArray, 6); // notFound = -6
  • Fill an array with a specific value:

The Arrays.fill() method fills an array with a specified value.

int[] intArray = new int[5];
Arrays.fill(intArray, 7); // intArray becomes {7, 7, 7, 7, 7}
  • Copy an array:

The Arrays.copyOf() method creates a new array that is a copy of the original array, with a specified length.

int[] originalArray = {1, 2, 3, 4, 5};
int[] copiedArray = Arrays.copyOf(originalArray, originalArray.length); // copiedArray becomes {1, 2, 3, 4, 5}
  • Compare two arrays for equality:

The Arrays.equals() method compares two arrays for equality, checking if both arrays have the same number of elements and the same elements in the same order.

int[] array1 = {1, 2, 3};
int[] array2 = {1, 2, 3};
boolean isEqual = Arrays.equals(array1, array2); // isEqual = true
  • Convert an array to a string:

The Arrays.toString() method returns a string representation of the contents of the specified array.

int[] intArray = {1, 2, 3, 4, 5};
String arrayString = Arrays.toString(intArray); // arrayString = "[1, 2, 3, 4, 5]"

These are just a few of the many methods provided by the java.util.Arrays class. You can find more methods and their descriptions in the official Java documentation.

  1. Sorting Arrays with Arrays.sort() in Java: The Arrays.sort() method is used to sort arrays of primitive types and objects.

    int[] arr = {5, 2, 9, 1, 5, 6};
    Arrays.sort(arr);
    
  2. Searching Arrays with Arrays.binarySearch() in Java: The Arrays.binarySearch() method is used to search for an element in a sorted array.

    int[] arr = {1, 2, 5, 5, 6, 9};
    int index = Arrays.binarySearch(arr, 5);
    
  3. Comparing Arrays with Arrays.equals() in Java: The Arrays.equals() method compares the contents of two arrays for equality.

    int[] arr1 = {1, 2, 3};
    int[] arr2 = {1, 2, 3};
    boolean isEqual = Arrays.equals(arr1, arr2);
    
  4. Filling Arrays with Arrays.fill() in Java: The Arrays.fill() method is used to fill an array with a specific value.

    int[] arr = new int[5];
    Arrays.fill(arr, 10);
    
  5. Copying Arrays with Arrays.copyOf() in Java: The Arrays.copyOf() method creates a new array with a specified length and copies elements from the original array.

    int[] arr = {1, 2, 3};
    int[] copy = Arrays.copyOf(arr, arr.length);
    
  6. Range-based Operations with Arrays.copyOfRange() in Java: The Arrays.copyOfRange() method copies a specified range of elements from the original array.

    int[] arr = {1, 2, 3, 4, 5};
    int[] copy = Arrays.copyOfRange(arr, 1, 4); // Copies elements from index 1 to 3
    
  7. Converting Arrays to Strings with Arrays.toString() in Java: The Arrays.toString() method converts an array to its string representation.

    int[] arr = {1, 2, 3};
    String str = Arrays.toString(arr); // Produces "[1, 2, 3]"
    
  8. Checking Array Content with Arrays.deepEquals() in Java: The Arrays.deepEquals() method is used to compare multidimensional arrays for equality.

    int[][] arr1 = {{1, 2}, {3, 4}};
    int[][] arr2 = {{1, 2}, {3, 4}};
    boolean isEqual = Arrays.deepEquals(arr1, arr2);
    
  9. Parallel Sorting with Arrays.parallelSort() in Java: The Arrays.parallelSort() method performs parallel sorting on arrays.

    int[] arr = {5, 2, 9, 1, 5, 6};
    Arrays.parallelSort(arr);
    
  10. Creating a Parallel Prefix with Arrays.parallelPrefix() in Java: The Arrays.parallelPrefix() method is used to compute the prefix sum of elements in parallel.

    int[] arr = {1, 2, 3, 4, 5};
    Arrays.parallelPrefix(arr, (x, y) -> x + y);
    
  11. Java Arrays Class and Primitive Data Types: The Arrays class supports methods for primitive data types like int, long, double, etc.

    int[] intArray = {1, 2, 3};
    Arrays.sort(intArray);
    
    double[] doubleArray = {2.5, 1.0, 3.5};
    Arrays.sort(doubleArray);
    
  12. Working with Multidimensional Arrays in Java Arrays: The Arrays class provides methods to work with multidimensional arrays.

    int[][] arr = {{1, 2}, {3, 4}};
    int sum = Arrays.stream(arr).flatMapToInt(Arrays::stream).sum();
    
  13. Filtering Arrays with Java Arrays.stream() and Arrays.filter(): The Arrays.stream() method is used to create a stream from an array, and Arrays.filter() filters elements based on a predicate.

    int[] arr = {1, 2, 3, 4, 5};
    int[] filteredArr = Arrays.stream(arr).filter(x -> x % 2 == 0).toArray();
    
  14. Java Arrays Class and the List Interface: Arrays class works well with the List interface.

    List<String> list = Arrays.asList("apple", "banana", "orange");
    
  15. Using Arrays Utility Class in Java Examples: The Arrays class is versatile and widely used in Java programming for various array-related operations.

    int[] arr = {3, 1, 4, 1, 5, 9, 2, 6, 5};
    Arrays.sort(arr);
    
    int index = Arrays.binarySearch(arr, 4);
    
    int[] copy = Arrays.copyOfRange(arr, 1, 5);
    
    System.out.println(Arrays.toString(arr));