<< Chapter < Page Chapter >> Page >

The output

The code in Listing 8 produces the following output on the screen:

c8 c7 c6 c5 c14 c13 c12 c11

Note that the order of this data is generally descending, and there is no string encapsulating the characters c4 .

Sort and display the data

The code in Listing 9 uses the sort method of the Arrays class to sort the array data into ascending order.

Listing 9 . Sort and display the data.
Arrays.sort((Object[])v4);//Display the sorted data for(int i = 0; i<Array.getLength(v4); i++){ System.out.print(Array.get(v4, i) + " ");}//end for loop

The output

The code in Listing 9 displays the sorted contents of the array object, producing the following output on the computer screen :

c11 c12 c13 c14 c5 c6 c7 c8

Note that the order of the data in the array object has been modified, and the array data is now in ascending order.

(This order is based on the natural ordering of the String data. I discuss other ways to order sorted data in conjunction with the Comparable and Comparator interfaces in my modules on the Java Collections Framework .)

Binary search

A binary search is a search algorithm that can very quickly find an item stored in a sorted collection of items. In this case, the collection of items isstored in an array object, and the data is sorted in ascending order.

Search for an existing string

Listing 10 uses the binarySearch method of the Arrays class to perform a search for an existing String object whose reference is stored in the sorted array. The code searches for the reference to the String object encapsulating the characters c5 .

Listing 10 . Search for an existing string.
System.out.println(Arrays.binarySearch((Object[])v4,"c5"));

The result of the search

The code in Listing 10 displays the numeral 4 on the screen.

When the binarySearch method finds a match, it returns the index value of the matching element. If you go back and look at the sorted contents of the array shown earlier, you will see that this is the index of the element containing areference to a String object that encapsulates the characters c5 .

Search for a non-existing string

The code in Listing 11 uses the binarySearch method to search for a reference to a String object that encapsulates the characters c4 . As I indicated earlier, a String object that encapsulates these characters is not represented in the sorted array object.

Listing 11 . Search for a non-existing string.
System.out.println(Arrays.binarySearch((Object[])v4,"c4"));

The result of the search

The code in Listing 11 produces the following negative numeral on the screen: -5 .

Here is Sun's explanation for the value returned by the binarySearch method:

"Returns: index of the search key, if it is contained in the list; otherwise, (-(insertion point) - 1). The insertion point is defined as the pointat which the key would be inserted into the list: the index of the first element greater than the key, or list.size(), if all elements in the list are less thanthe specified key. Note that this guarantees that the return value will be>= 0 if and only if the key is found."

Thus, the negative return value indicates that the method didn't find a match. The absolute value of the return value can be used to determine the indexof the reference to the target object if it did exist in the sorted list. I willleave it as an exercise for the student to interpret Sun's explanation beyond this simple explanation.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask