Sub topic 9
Comparing and Printing Array Contents
Two arrays can hold the same values without being the same array. Java provides tools to check either question and to print the values in a readable form.
An array is an object: a value stored in memory. An array variable holds a reference, which points to that object. Each value in the array is an element.
Compare arrays
The == operator compares array references. It answers: “Do these variables point to the same array?”
To compare contents, use Arrays.equals. It checks that the arrays have the same length and matching elements in the same order. Arrays is a Java class with useful array methods. A method is an action you can call by name.
For arrays of objects, Arrays.equals compares elements using their equals methods. For example, String elements are compared by their text. A custom class may need its own equals method for a meaningful content comparison.
Print arrays
Use Arrays.toString to turn a one-dimensional array into readable text. For example, an array with the values 2 and 4 becomes [2, 4].
Printing an array directly does not normally show its elements. Instead, Java prints a description of the array object. Arrays.toString returns text; it does not change the array.
Compare and print nested arrays
A nested array holds arrays as its elements. For these arrays, Arrays.equals compares the row references, not the values inside each row.
Use Arrays.deepEquals to compare the contents of nested arrays. Use Arrays.deepToString to print their contents.
Try it
Save this program as CompareArrays.java:
import java.util.Arrays;
public class CompareArrays {
public static void main(String[] args) {
int[] first = {2, 4};
int[] second = {2, 4};
int[] alias = first;
System.out.println("Same array: " + (first == alias));
System.out.println("Separate arrays: " + (first == second));
System.out.println("Same contents: " + Arrays.equals(first, second));
System.out.println("Contents: " + Arrays.toString(first));
String[] namesA = {"Ada", "Lin"};
String[] namesB = {"Ada", "Lin"};
System.out.println("Same names: " + Arrays.equals(namesA, namesB));
int[][] rowsA = {{1, 2}, {3}};
int[][] rowsB = {{1, 2}, {3}};
System.out.println("Same rows: " + Arrays.equals(rowsA, rowsB));
System.out.println("Same nested contents: "
+ Arrays.deepEquals(rowsA, rowsB));
System.out.println("Nested contents: " + Arrays.deepToString(rowsA));
}
}Compile and run it:
javac CompareArrays.java
java CompareArraysThe output is:
Same array: true
Separate arrays: false
Same contents: true
Contents: [2, 4]
Same names: true
Same rows: false
Same nested contents: true
Nested contents: [[1, 2], [3]]first and alias point to the same array. first and second point to separate arrays, but their contents match.
The nested arrays have matching values too. However, their rows are separate arrays, so the row references do not match. That is why Arrays.equals returns false while Arrays.deepEquals returns true.
Exercise
Exercise
If int[] a = {5, 6}; and int[] b = {5, 6};, what do a == b and Arrays.equals(a, b) return?
Show answer
a == b returns false because the arrays are separate objects. Arrays.equals(a, b) returns true because their contents match.
Exercise
Which method prints the contents of int[][] rows: Arrays.toString(rows) or Arrays.deepToString(rows)?
Show answer
Arrays.deepToString(rows) prints the contents inside the rows.