• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

class inside a class problem

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all, I have never seen a class defined under another class ....I'm having problem in it please explain






first I want to know how class pqsort is defined under class pe ....


and how the comparator used in this example is sorting elements in reverse order



question is taken from Kathy Serra book....page 591

 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, please UseCodeTags (← click this) when posting code. I added them for you this time. Doesn't it look better?

Second, your code won't compile. You missed some braces and made some typos.

If it compiled this comparator would return negative value when first parameter is bigger than first parameter and positive value when first parameter is smaller than second parameter*.
This is exactly the opposite of Integer's natural sorting thus reverse order.

* I did not consider possible overflows here. This method of comparing integers is not advisable as it might suffer from overflow issues.
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Comparator would not sort anything. It would be passed as a resource to a method which does sort things. Something like thisObviously the array has to be a type compatible with the Comparator, i.e. Integer[].
As Paweł says, there are possible errors in that method. You can read about “ordering” in the Java Tutorials. It tells you about that error.
That Comparator is actually redundant, because Integer already implements the Comparable<Integer> interface, so you can say
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you copy the code exactly from the book? You have missed out the capital letters and putting the code all together like that without indenting makes it difficult to read. Correct indentation would have shown you the compiler errors which Paweł noticed earlier.
 
aman bisht
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
exact code is as follows :

class PQ{
static class PQsort implements Comparator<Integer>{
public int compare(Integer one,Integer two){
return two - one ;
}
}


please explain how a class PQsort can be defined under PQ class .....this thing is new to me
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

aman bisht wrote:
please explain how a class PQsort can be defined under PQ class .....this thing is new to me



This concept is an inner class... or in this example, specifically called a "nested" class. Here is the Oracle tutorial regarding it...

http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

Henry
 
aman bisht
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry sir...
 
Right! We're on it! Let's get to work tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic