• Post Reply Bookmark Topic Watch Topic
  • New Topic

class inside a class problem  RSS feed

 
aman bisht
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • 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

 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • 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: 56593
172
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks Henry sir...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!