• Post Reply Bookmark Topic Watch Topic
  • New Topic

comparable comparator  RSS feed

 
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying below examples
http://www.mkyong.com/java/java-object-sorting-example-comparable-and-comparator/

My code looks like




i am getting compilation errorn in Fruit class at line 36 saying compareFruit cannot be resolved to variable. How to fix this. please advise

Also as below





i wonder why we are not assigning below sorted output to something.
Arrays.sort(fruits);

Collections.sort(fruits);

without assigning how the sort is happening. please advise

As below i tried to implement Comparator for sorting on multiple fields



i am getting compilation error saying fruit is not valid variable at line 12 of TestFruitComparator . How to fix. please advise
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what is fruits in that line 12? Where is it declared?
 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


when i define local variable as above getting NPE

Exception in thread "main" java.lang.NullPointerException
at java.util.TimSort.sort(TimSort.java:184)
at java.util.Arrays.sort(Arrays.java:670)
at comparator.TestFruitComparator.main(TestFruitComparator.java:13)


Instead of array if write as below getting compilation error The method sort(T[], Comparator<? super T>) in the type Arrays is not applicable for the arguments (Fruit,
Comparator<Fruit>)

 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are going to try passing null it is hardly surprising if you get a null pointer exception. In the second example you are passing a Fruit rather than a Fruit array.
 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fruit is regular java class object right not Array object?
I am wondering how to declare 'fruit'

compiler not liking below line either saying Variable must provide either dimension expressions or an array initializer

Fruit[] fruits = new Fruit[];
 
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
sai rama krishna wrote:
I am wondering how to declare 'fruit'

compiler not liking below line either saying Variable must provide either dimension expressions or an array initializer

Fruit[] fruits = new Fruit[];


No offense, but is it a good idea to guess on the Java array syntax? Perhaps it would be good to start with the Oracle Java tutorial on arrays?

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

Henry
 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i got the syntax which says i should define the size of aray. But here Fruit is not array but java object. How do i resolve in my case. please advise
 
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
sai rama krishna wrote:i got the syntax which says i should define the size of aray. But here Fruit is not array but java object. How do i resolve in my case. please advise


Are you trying to create a Fruit instance? Or are you trying to create a Fruit Array instance? These are *not* the same thing.

If you are trying to create a Fruit array object, then you should define the size of the array.

If you are trying to create a Fruit object, then you only need to define what is needed by the constructor.

And if you are trying to use a Fruit object as an Fruit array object, or trying to use a Fruit array object as a Fruit object ... well ... don't do that...

Henry
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you followed the suggestion given earlier to look in the Java® Tutorials? Don't you have a Java® textbook which tells you how to create an array?
 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you trying to create a Fruit instance? Or are you trying to create a Fruit Array instance? These are *not* the same thing.

If you are trying to create a Fruit array object, then you should define the size of the array.

If you are trying to create a Fruit object, then you only need to define what is needed by the constructor.

And if you are trying to use a Fruit object as an Fruit array object, or trying to use a Fruit array object as a Fruit object ... well ... don't do that..


That link is not clear in the content. I am also wondering
 
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
sai rama krishna wrote:
That link is not clear in the content. I am also wondering


I don't know what to tell you. The Oracle tutorials are pretty basic -- so, there isn't much that can be elaborated. Perhaps, if you give it another try, and then ask us something that may be confusing you, we may be able to determine a hint that can help.

Henry
 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like there is mistake in the tutorial. I see comment as below


I tried changing as below



Still compiler not happy.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first code block is irrelevant; in that case fruits is a List rather than an array.
To repeat what I said earlier: have you looked in a book or the Java® Tutorials? That should tell you what you need to know.
 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

I read above tutorial which is simply telling declaring array, manipulating array. But not sure what will help me from that tutorial to solve this particular problem of mine. I do not know my problem well enough first of all to fix it here. I know something is wrong in the code mentioned in that link as per comments and my personal testimony.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have read the tutorial, which tells you how to initialise [empty] arrays, why are you not initialising your array correctly?
And do you know how to use an array initaliser? Have a look at the Java® Language Specification (=JLS). The JLS is not easy to read (not helped by their showing some very silly examples in that section) but there are some array initialisers in the lower half of the first diagram (10-2.1).
 
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
Campbell Ritchie wrote:If you have read the tutorial, which tells you how to initialise [empty] arrays, why are you not initialising your array correctly?


Agreed. This has nothing to do with a "particular problem". The array declaration is simply not valid Java syntax -- and this is a compile error condition, regardless of any problem that you are trying to solve.

In other words, you are not writing valid Java syntax -- and that is what the tutorial is supposed to teach you.

Henry
 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I initialized to random size of 15.

Now i see below error at line 14

The method sort(List<T>, Comparator<? super T>) in the type Collections is not applicable for the arguments (Fruit[], Comparator<Fruit>)

sort() expecting List not array as below

public static <T> void sort(List<T> list, Comparator<? super T> c) {
Object[] a = list.toArray();
Arrays.sort(a, (Comparator)c);
ListIterator i = list.listIterator();
for (int j=0; j<a.length; j++) {
i.next();
i.set(a[j]);
}
}
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look in the API documentation what this sort method sorts and what the other sort method sorts.
 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
one takes Array as input other sort() takes List as input.
 
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
sai rama krishna wrote:
Now i see below error at line 14

The method sort(List<T>, Comparator<? super T>) in the type Collections is not applicable for the arguments (Fruit[], Comparator<Fruit>)

sort() expecting List not array as below


Not sure of your question -- as you seem to already know what is wrong with your code.

sai rama krishna wrote:one takes Array as input other sort() takes List as input.


Correct. And obviously, you can't pass an array when it expects a list. You already know what is wrong. So, what is your question / issue?

Henry
 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My question is

I have below Fruit class


I want to write a simile java application to test it but not able to test it


How do i tweak my test class so that it sorts Fruit class properly.
I know what is wrong but do not know how to fix.Please advise
 
Ranch Foreman
Posts: 3074
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As has been pointed out, you are passing an array into a method that expects a List.
This will not work.

Why are you not using Arrays.sort, since this is an array?

Also, your array of Fruits is empty. That is 15 entries will be null, which will possibly result in a NullPointerException.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you remember the previous posts where you found out what sort of object you can pass to Collections#sort and what sort of object you can pass to Arrays#sort?
 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes. I remember it. I also just read below link
http://www.questionscompiled.com/answer/java/66/difference-between-arrays-sort-and-collections-sort-methods/
which says
The Collections.sort() method takes a list implementation as input while Arrays.sort() takes an array as input.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!