Published on December 2016 | Categories: Documents | Downloads: 30 | Comments: 0 | Views: 284
of 4
Download PDF   Embed   Report



Displaying the Contents of an Array
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:

will produce the result:
[22, 66, 33, 77, 99, 55, 11, 44, 88]

Although the Arrays.toString method does not work very well with multidimensional arrays:

will result in a not very pleasant looking representation of the array. Something like:
[[C@420a52f, [C@7b3cb2c6, [C@4dfd245f, [C@265f00f9]

To display the contents of multidimensional arrays use the Arrays.deepToString method:

which displays:
[[b, e, a, r], [c, a, r, p], [f, a, r, m], [d, a, w, n]]

Comparing Two Arrays
Although an array has the equals method it does not check to see if the contents of two arrays are equal but if the two arrays are stored in the same pace in memory. So, if we make another int array which has exactly the same elements as numbersArray and then call the equals method:
int[] duplicateNumbersArray = {22,66,33,77,99,55,11,44,88}; System.out.println(numbersArray.equals(duplicateNumbersArray));

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:

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.

Filling the Elements of an Array
The elements of an array can all be filled with the same value by using the Arrays.fill method. Like the sort method, all the elements can be included or you can define a start and ending position for the fill. To fill the numbersArray with zeros:
Arrays.fill(numbersArray, 0); System.out.println(Arrays.toString(numbersArray));

will result in:
[0, 0, 0, 0, 0, 0, 0, 0, 0]

To fill the elements at position 3 to 8 with zeros:
Arrays.fill(numbersArray, 3, numbersArray.length, 0); System.out.println(Arrays.toString(numbersArray));

which results in:
[11, 22, 33, 0, 0, 0, 0, 0, 0]

The fill method does not work for multidimensional arrays.

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));

which returns the positional value of:

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:

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));

which results in an array with twenty elements:
[22, 66, 33, 77, 99, 55, 11, 44, 88, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

To make a copy of the numberArray with less elements:
int[] shorterNumbersArray = Arrays.copyOf(numbersArray, 4); System.out.println(Arrays.toString(shorterNumbersArray));

which results in an array withe the first 4 elements of the numbersArray:
[22, 66, 33, 77]

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));

which results in the elements at position 3 to 6 in the new array:
[77, 99, 55, 11]

Sponsor Documents

Or use your account on


Forgot your password?

Or register your new account on


Lost your password? Please enter your email address. You will receive a link to create a new password.

Back to log-in