← Back to Core Java Course | Chapter 5: Methods & Arrays | Lesson 10 of 10

Java Arrays Class

The Arrays class is a toolbox of helpers for arrays, like printing them, sorting, searching, filling, and copying. It saves you writing those loops yourself.
Syntax
java
import java.util.Arrays;

Arrays.sort(array);
Arrays.toString(array);
Arrays.fill(array, value);

Printing Arrays with toString()

Printing an array directly with System.out.println(arr) shows something like [I@1b6d3586 — the array's type and memory hash, not its contents — because arrays don't override toString().

Arrays.toString(arr) from java.util.Arrays gives you the readable [1, 2, 3] form instead.

Example: Printing Arrays with toString()

java
import java.util.Arrays;
public class Main {
	public static void main(String[] args) {
		int[] arr = {1, 2, 3};
		System.out.println(arr); // e.g. [I@1b6d3586
		System.out.println(Arrays.toString(arr)); // [1, 2, 3]
	}
}

Sorting Arrays

Arrays.sort() sorts a primitive array in place in ascending order using a tuned dual-pivot quicksort, and it also works on object arrays (like String[]) as long as the elements implement Comparable or you supply a Comparator.

Example: Sorting Arrays

java
// Import java.util.Arrays so it can be used by its short name
import java.util.Arrays;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Declare arr and set it to {5, 2, 8, 1}
		int[] arr = {5, 2, 8, 1};
		Arrays.sort(arr);
		// Print a line to the console
		System.out.println(Arrays.toString(arr));
	}
}

Searching with Binary Search

Arrays.binarySearch() finds an element's index in O(log n) time, but only works correctly if the array is already sorted — running it on an unsorted array gives an undefined, unreliable result rather than a clear error.

Example: Searching with Binary Search

java
// Import java.util.Arrays so it can be used by its short name
import java.util.Arrays;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Declare arr and set it to {1, 3, 5, 7, 9}
		int[] arr = {1, 3, 5, 7, 9};
		// Declare index and set it to Arrays.binarySearch(arr, 7)
		int index = Arrays.binarySearch(arr, 7);
		// Print a line to the console
		System.out.println(index);
	}
}

Filling Arrays

Arrays.fill() overwrites every element (or a specified range) with the same value in one call, which is convenient for resetting a counting array or initializing a buffer to a sentinel value like -1 before real data is written in.

Example: Filling Arrays

java
// Import java.util.Arrays so it can be used by its short name
import java.util.Arrays;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Declare arr and set it to new int[5]
		int[] arr = new int[5];
		Arrays.fill(arr, -1);
		// Print a line to the console
		System.out.println(Arrays.toString(arr));
	}
}

Copying Arrays

Arrays.copyOf() creates a new array with the same or a different length, copying over as many original elements as will fit and padding the rest with default values if the new array is larger.

This is one common way Java code implements a manual resize since arrays themselves can't grow.

Example: Copying Arrays

java
// Import java.util.Arrays so it can be used by its short name
import java.util.Arrays;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Declare original and set it to {1, 2, 3}
		int[] original = {1, 2, 3};
		// Declare copy and set it to Arrays.copyOf(original, 5)
		int[] copy = Arrays.copyOf(original, 5);
		// Print a line to the console
		System.out.println(Arrays.toString(copy));
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Printing an array directly with System.out.println(arr), which shows something like [I@1b6d3586; use Arrays.toString(arr).
  2. Using Arrays.binarySearch on an array that is not sorted, which gives wrong results.
  3. Expecting Arrays.asList() on a primitive int[] to make a list of ints.
Chapter Summary
  • Methods package reusable code, take parameters, return values, and can be overloaded, recursive, static, or use varargs.
  • Arrays store several values of one type, including multi-dimensional arrays.
  • The Arrays class provides helpful utilities for working with array data.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.