• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

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

 
Ranch Hand
Posts: 49
Eclipse IDE Spring Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 2447
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 49
Eclipse IDE Spring Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
reply
    Bookmark Topic Watch Topic
  • New Topic