Shiklo Programming
EN
  • English
  • Español
  • Português
  • Deutsch
  • Français
  • 日本語
  • हिन्दी

Java Arrays Class

Writing a loop every time you want to print or sort an array gets tiring. The java.util.Arrays class has ready-made methods for the common jobs. Like Scanner, it is in java.util, so the program needs an import line.

Printing an array

Printing an array directly gives something unreadable like [I@1b6d3586 (its type and a code). Arrays.toString gives the values:

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] numbers = {5, 3, 9, 1};
        System.out.println(Arrays.toString(numbers));
        String[] names = {"Zara", "Amit", "Leo"};
        System.out.println(Arrays.toString(names));
    }
}
Output
[5, 3, 9, 1]
[Zara, Amit, Leo]

Sorting and searching

Arrays.sort sorts the array itself, from small to large (or alphabetically for strings). On a sorted array, Arrays.binarySearch finds the index of a value very quickly:

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] numbers = {42, 7, 19, 3, 25};
        Arrays.sort(numbers);
        System.out.println(Arrays.toString(numbers));
        System.out.println(Arrays.binarySearch(numbers, 19));
        String[] cities = {"Pune", "Agra", "Kochi"};
        Arrays.sort(cities);
        System.out.println(Arrays.toString(cities));
    }
}
Output
[3, 7, 19, 25, 42]
2
[Agra, Kochi, Pune]

binarySearch only works on a sorted array. If the value is not there, it returns a negative number.

Filling and copying

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] scores = new int[5];
        Arrays.fill(scores, 10);
        System.out.println(Arrays.toString(scores));
        int[] original = {1, 2, 3, 4, 5};
        int[] firstThree = Arrays.copyOf(original, 3);
        int[] bigger = Arrays.copyOf(original, 7);
        int[] middle = Arrays.copyOfRange(original, 1, 4);
        System.out.println(Arrays.toString(firstThree));
        System.out.println(Arrays.toString(bigger));
        System.out.println(Arrays.toString(middle));
    }
}
Output
[10, 10, 10, 10, 10]
[1, 2, 3]
[1, 2, 3, 4, 5, 0, 0]
[2, 3, 4]

Copying is how you "grow" an array: since its length is fixed, you make a new, longer copy. The ArrayList topic shows a list that grows by itself.

Comparing arrays

== on two arrays only checks whether they are the very same array object. To compare the contents, use Arrays.equals:

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] a = {1, 2, 3};
        int[] b = {1, 2, 3};
        System.out.println(a == b);
        System.out.println(Arrays.equals(a, b));
    }
}
Output
false
true

Sum, minimum and maximum

Arrays.stream turns an array into a stream of values with handy methods. Streams get their own topic, Streams, later; these three are useful right away:

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] marks = {67, 89, 74, 95, 58};
        System.out.println("Sum: " + Arrays.stream(marks).sum());
        System.out.println("Max: " + Arrays.stream(marks).max().getAsInt());
        System.out.println("Average: " + Arrays.stream(marks).average().getAsDouble());
    }
}
Output
Sum: 383
Max: 95
Average: 76.6

When to use these methods

Reach for the Arrays class before writing a loop of your own: its methods are tested, fast and easy to read. A loop is still the right tool when you need something special, such as counting only some values or stopping early, as in the Arrays topic.

Practice exercises

  1. Sort the array {"pear", "apple", "fig", "banana"} and print it.
  2. Make an array of 10 numbers all equal to 7 with Arrays.fill and print it.
  3. Print the smallest value of {12, -4, 33, 0, 8} using Arrays.stream(...).min().