Displaying The Contents of An Array: Object
Displaying The Contents of An Array: Object
The toString method of an array is not very useful in displaying what's in an array. Instead it displays a representation of the array object. However, the toString method of the Arrays class will display the contents of an array inside two square brackets:
System.out.println(Arrays.toString(numbersArray));
Although the Arrays.toString method does not work very well with multidimensional arrays:
System.out.println(Arrays.toString(characterArray));
will result in a not very pleasant looking representation of the array. Something like:
[[C@420a52f, [C@7b3cb2c6, [C@4dfd245f, [C@265f00f9]
which displays:
[[b, e, a, r], [c, a, r, p], [f, a, r, m], [d, a, w, n]]
the result of the equals method will always be false. The only way for this equals method to return true is if the two arrays are pointing to the same array in memory:
int[] sameArray = numbersArray; //sameArray will be given the same memory reference as numbersArray //i.e. the sameArray and numbersArray variables point to exactly the same array System.out.println(numbersArray.equals(sameArray));
This means to test the equality for two one-dimensional arrays the Arrays.equals method must be used:
System.out.println(Arrays.equals(numbersArray, duplicateNumbersArray));
And as you might have already guessed the Arrays.equals method does not work for multidimensional arrays. In this case use the Arrays.deepEquals method:
char[][] duplicateCharacterArray = {{'b','e','a','r'}, {'c','a','r','p'}, {'f','a','r','m'}, {'d','a','w','n'}}; System.out.println(Arrays.deepEquals(characterArray,duplicateCharacterArray)) ;
Sorting Arrays
Sorting one-dimensional arrays can be achieved by using the Arrays.sort method. It comes with two implementations. The first sorts the entire array in ascending order:
Arrays.sort(numbersArray); System.out.println(Arrays.toString(numbersArray));
resulting in:
[11, 22, 33, 44, 55, 66, 77, 88, 99]
The second takes two extra arguments that define the position of the starting and end element you want to be sorted. Here only the elements from position 3 to 7 are sorted in ascending order:
Arrays.sort(numbersArray,3,8); System.out.println(Arrays.toString(numbersArray));
resulting in:
[22, 66, 33, 11, 44, 55, 77, 99, 88]
Note: A quirk of the second implementation of the sort method is the starting position is inclusive and the ending position is exclusive. This means to define the last element to be included in the sort you need to enter in a position value that doesn't exist in the array (i.e., the last element in the numbersArray is in position 8 but for the sort method you need to enter in 9). To avoid confusion its best to just use the length field to define the last element:
Arrays.sort(numbersArray,3,numbersArray.length);
To sort the elements of a multidimensional array you have to define the comparison operation you want to employ otherwise the compiler will not know how to sort the array and will raise an error.
Searching Arrays
The Arrays class does provide a search method called binarySearch for finding the position of an element containing a certain value. However, the search method only works if the array has previously been sorted. If it hasn't then the results are unpredictable. Similarly if all the values in the array aren't unique then it's not possible to tell which element will be returned (i.e., if the numbersArray had two elements containing the number 4 then the search might return the position of either one). To find the value 66 in the numbersArray:
System.out.println(Arrays.binarySearch(numbersArray, 66));
If you search for a value not in the array a negative value will be returned:
System.out.println(Arrays.binarySearch(numbersArray, 23));
which returns:
-3
The value returned shows where the search expected the value to be.
Copying an Array
The Arrays class also provides two methods for copying an array - the copyOf and copyOfRange methods. The copyOf method returns a new array which can be longer or shorter than the original through the passing of a length parameter. If the length parameter is shorter than the length of the original array then the contents of the new array are a truncation of the original array. If the length parameter is longer than the length of the original array then the extra elements are filled with a null value. To make a copy of the numbersArray with more elements:
int[] longerNumbersArray = Arrays.copyOf(numbersArray, 20); System.out.println(Arrays.toString(longerNumbersArray));
The copyOfRange method allows the definition of a range of elements to be copying into the new array. It takes the value of the start and end positions of the elements to be copied. Again the start position is inclusive and the end position is exclusive:
int[] rangedNumbersArray = Arrays.copyOfRange(numbersArray, 3,7); System.out.println(Arrays.toString(rangedNumbersArray));