The Arrays class: print, sort and compare
20 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)Java's Arrays class has ready-made methods for common array jobs. Arrays.toString prints an array, Arrays.sort puts it in order and Arrays.equals compares two arrays.
Printing with Arrays.toString
System.out.println(marks) prints something like [I@1b6d3586, not the marks. Arrays.toString(marks) gives the values in square brackets; add import java.util.Arrays; at the top, as for Scanner (opens in a new tab).
Example 1 · println vs Arrays.toString
import java.util.Arrays;
public class PrintArray { public static void main(String[] args) { int[] marks = {78, 85, 62, 90, 71}; System.out.println(marks); System.out.println(Arrays.toString(marks)); }}[I@1b6d3586 [78, 85, 62, 90, 71]
What this code does
- The first line is
[I@and a code:[Imeans "int array" and the code tells where it is in memory. Yours will differ, and it can change on every run. Arrays.toStringgives the values, so use it whenever you want to see what is inside.
For a 2D array, Arrays.deepToString(table) prints every row, like [[78, 85], [66, 71]].
Sorting with Arrays.sort
A leaderboard needs the scores in order. Arrays.sort(scores); puts the elements in order inside the same array: numbers smallest first, text from A to Z.
Example 2 · Sort numbers and names
import java.util.Arrays;
public class SortScores { public static void main(String[] args) { int[] scores = {45, 12, 99, 56, 78}; Arrays.sort(scores); System.out.println(Arrays.toString(scores)); System.out.println("Lowest: " + scores[0]); System.out.println("Highest: " + scores[scores.length - 1]); String[] names = {"Priya", "Arjun", "Meena", "Ravi"}; Arrays.sort(names); System.out.println(Arrays.toString(names)); }}[12, 45, 56, 78, 99] Lowest: 12 Highest: 99 [Arjun, Meena, Priya, Ravi]
What this code does
Arrays.sortchangesscoresitself; no new array is made and nothing is returned.- After sorting, the smallest is at index 0 and the largest at the last index, so no search loop is needed.
sort returns nothing, so int[] s = Arrays.sort(a); does not compile; just write Arrays.sort(a);. Capital letters sort before small ones, so "Zoya" comes before "arjun". For largest first, read the sorted array backwards.
Comparing: == or Arrays.equals
A student's answers must match the answer key. a == b only asks whether a and b are the same array; to compare the values, in order, use Arrays.equals(a, b).
Example 3 · Check the answers
import java.util.Arrays;
public class CheckAnswers { public static void main(String[] args) { int[] key = {2, 1, 3, 4}; int[] answers = {2, 1, 3, 4}; System.out.println(key == answers); System.out.println(Arrays.equals(key, answers)); answers[3] = 1; System.out.println(Arrays.equals(key, answers)); }}false true false
What this code does
keyandanswersare two different arrays, sokey == answersisfalseeven though their values match.Arrays.equalscompares box by box:truewhile all four match,falseonce the last answer changes.
Filling and copying
A new seat chart should start with every seat booked, and a backup must not change with the original. Arrays.fill(a, 1) sets every box; Arrays.copyOf(a, n) makes a new array with the first n values.
Example 4 · Fill, copy and grow
import java.util.Arrays;
public class FillCopy { public static void main(String[] args) { int[] seats = new int[5]; Arrays.fill(seats, 1); System.out.println(Arrays.toString(seats)); int[] marks = {10, 20, 30}; int[] backup = Arrays.copyOf(marks, marks.length); marks[0] = 99; System.out.println(Arrays.toString(marks) + " " + Arrays.toString(backup)); int[] bigger = Arrays.copyOf(backup, 5); System.out.println(Arrays.toString(bigger)); }}[1, 1, 1, 1, 1] [99, 20, 30] [10, 20, 30] [10, 20, 30, 0, 0]
What this code does
Arrays.fill(seats, 1)sets all 5 boxes to 1 in one line.backupis a real copy, so changingmarks[0]leaves it alone (unlikeint[] backup = marks;, from Arrays and methods (opens in a new tab)).Arrays.copyOf(backup, 5)keeps the 3 old values and adds two boxes holding 0.
A bigger n makes a bigger array: the old values come first and the new boxes hold 0. That is how you "grow" an array whose size is fixed.
Classify
Which Arrays method?
Tap a job, then tap the method that does it.
Next step
Check your understanding
9 quick questions