aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes sort() method from Arrays class question and another regarding inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "sort() method from Arrays class question and another regarding inner class" Watch "sort() method from Arrays class question and another regarding inner class" New topic
Author

sort() method from Arrays class question and another regarding inner class

David Samer
Ranch Hand

Joined: Feb 08, 2012
Posts: 49

Greetings once again everyone

I have reviewed chapter 8 from Kathy's and Bert's book , taking the corresponding test and there's a question which has got my attention with an issue.

Page 700 , question (answered) 12 , from the book. ( It's the solved questions with explanation page).

Given:


What is the result?
A. Compilation fails
B. button key lint nickel
nickel lint key button
C. nickel button key lint
button key lint nickel
D. nickel button key lint
nickel button key lint
E. nickel button key lint
nickel lint key button
F. An exception is thrown at runtime

Answer:
✓ A is correct, the inner class Sorter must be declared static to be called from the static
method main(). If Sorter had been static, answer E would be correct.
B, C, D, E, and F are incorrect based on the above. (Objectives 1.1, 1.4, 6.5)


Inner class question
------------------------

As it it Sorter inner class, it only can be accessed changing to static compare method . Would it be valid if I want to access method through this way? -> Pockets.Sorter s = Pockets.new Sorter () ; ( or do I get same error cause I am inside static main method? ) .

And the second question regarding sort method of Arrays class:

It says , changing compare method to static in Sorter inner class , answer would be E.

I really have tested in my IDE different options in order to know how array is sorted but I really can't get it clear enough. How does it work?

!Oh! by the way, what are the differences between "sorted" and "ordered" ?

Thanks in advance.
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 801
    
    1
You can create a Sorter instance: Pocket.Sorter s = new Pocket(). new Sorter();

In Sorter example, inside the compare method, the compareTo method returns -1 if a < b, it returns 1 if b>a. It returns 0 if a=b.
The Arrays.sort method swap two strings in the array if the compare method returns 1. That is sorting in descending order.

If you change the code:


By definition, "sorted" means the values in an array, list, tree are in ascending or descending based on how you define compare method for the object type you are going to sort.
By definition, "ordered" means the the order of the data in a list are put in the order when they are put in to the array or list.
For example, 1st number is put as the 1st in the arrray, 2nd number is put as the 2nd in the array...

When you iterate a hash set, you will see the order of its iteration is not guaranteed. You first hash "1" in the hash set and then "2" in the set. When you iterate it, "2" may be output first before "1".
Hash set is just like a post officer who puts mails into neighbour's mailboxes. The officer may not sort the mails and put mails for the 1st floor neighours' first before the 2nd floor neighbours.
Instead, the officer picks one mail. If the mail is for the 10th floor neighbour, then put it in the right mailbox. If the next mail is for the 4th floor, then put it there and etc.

David Samer
Ranch Hand

Joined: Feb 08, 2012
Posts: 49

Himai Minh wrote:You can create a Sorter instance: Pocket.Sorter s = new Pocket(). new Sorter();

In Sorter example, inside the compare method, the compareTo method returns -1 if a < b, it returns 1 if b>a. It returns 0 if a=b.
The Arrays.sort method swap two strings in the array if the compare method returns 1. That is sorting in descending order.

If you change the code:


By definition, "sorted" means the values in an array, list, tree are in ascending or descending based on how you define compare method for the object type you are going to sort.
By definition, "ordered" means the the order of the data in a list are put in the order when they are put in to the array or list.
For example, 1st number is put as the 1st in the arrray, 2nd number is put as the 2nd in the array...

When you iterate a hash set, you will see the order of its iteration is not guaranteed. You first hash "1" in the hash set and then "2" in the set. When you iterate it, "2" may be output first before "1".
Hash set is just like a post officer who puts mails into neighbour's mailboxes. The officer may not sort the mails and put mails for the 1st floor neighours' first before the 2nd floor neighbours.
Instead, the officer picks one mail. If the mail is for the 10th floor neighbour, then put it in the right mailbox. If the next mail is for the 4th floor, then put it there and etc.



There it is. Good explanation, now I do understand it, thank you Himai , great help.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: sort() method from Arrays class question and another regarding inner class