Java has an “array” class that offers many useful methods that you can use in your grade 12 PAT. All the methods are static i.e. “Array.nameOfTheMethod”. Here is a list of methods you mind find useful . . .

  • Arrays.toString(original array)
  • Arrays.sort(original array)
  • Arrays.fill(object,value)
  • Arrays.binarySearch(object,value)
  • Arrays.equals(object, object)

toString returns a string representation of the array. Example: [bird, cat, flower]. Useful for troubleshooting.

sort returns the whole array in ascending order.

fill assigns the fill value into each indexed postion of the array. Useful for inserting default values.

binarySearch searches the array of the specified value. Only for arrays that are already sorted.

equals checks to see is the contents of the two arrays are the same. Use on arrays that are already sorted so that the elements are in the same order. Also note that the method is case sensitive ie “Flower” is not equal to “flower”. NB Do not confuse this with the “equals” method when comparing Strings. 

Example One:

String[] myArray = new String[] {“bird”, “cat”, “flower”};
String stringArray = Arrays.toString(myArray);
System.out.println(“The Array: ” + stringArray);

Output looks like this: The Array: [bird, cat, flower]

Example Two:

String[] myArr = new String[] {“bird”, “cat”, “flower”};
String[] myArray = new String[] {“bird”, “cat”, “flower”};
boolean equalOrNot = Arrays.equals(myArr, myArray); // elements must be in the same order and the same case.
System.out.println(“Equal or not: ” + equalOrNot);

Output looks like this: Equal or not: true

Example Three:

String[] myArr = new String[] {“flower”, “bird”, “cat”, “flower”};
String[] myArray = new String[] {“flower”, “bird”, “cat”, “flower”};
Arrays.sort(myArr);
System.out.println(“Sorted array: ” + Arrays.toString(myArr));
System.out.println(“Unsorted array: ” + Arrays.toString(myArray));

Output looks like this: 

Sorted array: [bird, cat, flower, flower]
Unsorted array: [flower, bird, cat, flower]