AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Sorting arrays (again)

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help
View previous topic :: View next topic  
Author Message
Mike B
Guest





PostPosted: Fri Nov 26, 2004 9:12 pm    Post subject: Sorting arrays (again) Reply with quote



I wrote an assignment with sorting two arrays. One array had to be sorted in
reverse (descending order). I did this by changing the compareTo() method in
the class for that array type. I simply reversed the comparison order from
the normal.

But there is a different method for sorting arrays, this one:

static void sort(Object[] a, int fromIndex, int toIndex, Comparator c

I tried writing a class (CompareProducts) that implement the Comparator
interface, and am now trying to get my code to use this method, but I can't
seem to code the statement correctly. I've tried the following:

=> Arrays.sort(productOrderArray, 0, intProductCount, Comparator
CompareProducts);

E:BCIS 3630Assignment 7Prob7Customer.java:91: ')' expected
Arrays.sort(productOrderArray, 0, intProductCount, Comparator
CompareProducts);

^
1 error

=> Arrays.sort(productOrderArray, 0, intProductCount, CompareProducts);

E:BCIS 3630Assignment 7Prob7Customer.java:91: cannot find symbol
symbol : variable CompareProducts
location: class Prob7Customer
Arrays.sort(productOrderArray, 0, intProductCount, CompareProducts);
^
1 error


=> Arrays.sort(productOrderArray, 0, intProductCount, new CompareProducts);

E:BCIS 3630Assignment 7Prob7Customer.java:91: '(' or '[' expected
Arrays.sort(productOrderArray, 0, intProductCount, new CompareProducts);

^
E:BCIS 3630Assignment 7Prob7Customer.java:91: ')' expected
Arrays.sort(productOrderArray, 0, intProductCount, new CompareProducts);

^
2 errors


=> Arrays.sort(productOrderArray, 0, intProductCount, new
CompareProducts());

Note: E:BCIS 3630Assignment 7Prob7Customer.java uses unchecked or unsafe
operations.
Note: Recompile with -Xlint:unchecked for details.

Tool completed successfully

So I am about out of options.

If anyone is interested, here is the source for CompareProducts. Oh, and
btw. How can I implement the equals method? To what am I supposed to
compare? I tried this. for a 2nd value, but that did not seem to work. Some
illumination on that will also be appreciated.

import java.util.Comparator;

public class CompareProducts implements Comparator
{
public CompareProducts()
{
}

public int compare(Object o1, Object o2)
{
Prob7ProductOrder order1 = (Prob7ProductOrder) o1;
Prob7ProductOrder order2 = (Prob7ProductOrder) o2;

int val1 = Integer.parseInt(order1.getProductID());
int val2 = Integer.parseInt(order2.getProductID());

System.out.println("In CompareProducts.compare(). Val1:" + val1 + "
Val2:" + val2);

if (val1 < val2)
return -1;
if (val1 > val2)
return 1;
return 0;
}
// public boolean equals(Object o)
// {
// Prob6ProductOrder cust2 = (Prob6ProductOrder) o;
//
// int val1 = Integer.parseInt(this.getProductID());
// int val2 = Integer.parseInt(cust2.getProductID());
//
// System.out.println("In Prob6Product.compareTo(). Val1:" + val1 + "
Val2:" + val2);
//
// if (val1 == val2)
// return true;
// return false;
//
// }
}

My compareTo method in class Prob7.ProductOrder looks as follows:

public int compareTo(Object o)
{
Prob7ProductOrder order1 = (Prob7ProductOrder) o;

int val1 = Integer.parseInt(order1.getProductID());
int val2 = Integer.parseInt(this.getProductID());

if (val1 < val2)
return -1;
if (val1 > val2)
return 1;
return 0;
}

and works, I'm just killing myself trying to find the syntax to implement
the Comparator.





--
Mike B


Back to top
Bjorn Abelli
Guest





PostPosted: Fri Nov 26, 2004 10:14 pm    Post subject: Re: Sorting arrays (again) Reply with quote




"Mike B" wrote...

Quote:
I wrote an assignment with sorting two arrays.

=> Arrays.sort(productOrderArray, 0, intProductCount, Comparator
CompareProducts);

"Comparator CompareProducts" would be similar to
writing "Prob7ProductOrder[] productOrderArray"
in the arguments.

Quote:
=> Arrays.sort(productOrderArray, 0, intProductCount, CompareProducts);

That would be like writing "int" instead of "intProductCount".

Quote:
=> Arrays.sort(productOrderArray, 0, intProductCount, new
CompareProducts);

Now you're getting close...

But when instantiating an object, you need to tell what constructor to use,
which you do by assigning some arguments or an empty list...

Quote:
=> Arrays.sort(productOrderArray, 0, intProductCount, new
CompareProducts());

....as you finally did in the last example.


Quote:
Note: E:BCIS 3630Assignment 7Prob7Customer.java uses
unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

Tool completed successfully

Which means that it worked! Note that it didn't write out any *errors*, just
*notes*.

I guess you're using the JDK 5.0, in which generics are implemented. With
that you can make your application "more" typesafe if you want to. It just
tells you that though the compilation went through, there are things it
beleives you could improve in your code.

Quote:
If anyone is interested, here is the source for
CompareProducts. Oh, and btw. How can I implement the
equals method? To what am I supposed to compare?

equals in a Comparator is to compare with other Comparators!

NOT with what it is constructed to compare.

In your example the equals of CompareProducts should be more in the line of:

public boolean equals(Object o)
{
try
{
CompareProducts c = (CompareProducts) o;
return true;
}
catch (ClassCastException cce)
{
return false;
}
}

// Bjorn A



Back to top
Bjorn Abelli
Guest





PostPosted: Fri Nov 26, 2004 10:19 pm    Post subject: Re: Sorting arrays (again) Reply with quote




"Bjorn Abelli" wrote...
Quote:

"Mike B" wrote...

I wrote an assignment with sorting two arrays.

=> Arrays.sort(productOrderArray, 0, intProductCount, Comparator
CompareProducts);

"Comparator CompareProducts" would be similar to
writing "Prob7ProductOrder[] productOrderArray"
in the arguments.

Ahem, talking about mixing apples and oranges.

Writing "Comparator CompareProducts" is rather to write "Object[]
Prob7ProductOrder[]" instead of "productOrderArray".

Which is just as wrong...

// Bjorn A



Back to top
Mike B
Guest





PostPosted: Fri Nov 26, 2004 11:29 pm    Post subject: Re: Sorting arrays (again) Reply with quote

Bjorn Abelli <DoNotSpam.bjorn_abelli (AT) hotmail (DOT) com> wrote:
Quote:
"Mike B" wrote...
Note: E:BCIS 3630Assignment 7Prob7Customer.java uses
unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

Tool completed successfully

Which means that it worked! Note that it didn't write out any
*errors*, just *notes*.

I guess you're using the JDK 5.0, in which generics are implemented.
With that you can make your application "more" typesafe if you want
to. It just tells you that though the compilation went through, there
are things it beleives you could improve in your code.

Thank you for your explanation, it really was helpful. I'm curious as to
what the compiler think may be wrong or unchecked. How would I construct the
javac command to uncheck -Xlint (I tried and got an error message)?

Quote:

If anyone is interested, here is the source for
CompareProducts. Oh, and btw. How can I implement the
equals method? To what am I supposed to compare?

equals in a Comparator is to compare with other Comparators!

NOT with what it is constructed to compare.

In your example the equals of CompareProducts should be more in the
line of:
public boolean equals(Object o)
{
try
{
CompareProducts c = (CompareProducts) o;
return true;
}
catch (ClassCastException cce)
{
return false;
}
}


And now I can implement the equals method as well. It seems to me that it is
merely checking the type of object? Is that correct?
Quote:

// Bjorn A

--
Mike B



Back to top
Mike B
Guest





PostPosted: Fri Nov 26, 2004 11:47 pm    Post subject: Re: Sorting arrays (again) Reply with quote

Mike B <mrcics2000-news-nomail (AT) nomail (DOT) yahoo.com> wrote:
Quote:
Bjorn Abelli <DoNotSpam.bjorn_abelli (AT) hotmail (DOT) com> wrote:
"Mike B" wrote...

Thank you for your explanation, it really was helpful. I'm curious as
to what the compiler think may be wrong or unchecked. How would I
construct the javac command to uncheck -Xlint (I tried and got an
error message)?

Belay that request, I got it fixed. I had a wrong javac in my command
window.

Here is the output from running with -Xlint:unchecked:


E:BCIS 3630Assignment 7>C:"Program
Files"Javajdk1.5.0binjavac -Xlint:unchecked Prob7Customer.java
Prob7Customer.java:91: warning: [unchecked] unchecked conversion
found : CompareProducts
required: java.util.Comparator<? super Prob7ProductOrder>
Arrays.sort(productOrderArray, 0, intProductCount, new
CompareProducts());
^
Prob7Customer.java:91: warning: [unchecked] unchecked method invocation:
<T>sort
(T[],int,int,java.util.Comparator<? super T>) in java.util.Arrays is applied
to (Prob7ProductOrder[],int,int,CompareProducts)
Arrays.sort(productOrderArray, 0, intProductCount, new
Compare
Products());
^
2 warnings

--
Mike B



Back to top
Bjorn Abelli
Guest





PostPosted: Sat Nov 27, 2004 12:03 am    Post subject: Re: Sorting arrays (again) Reply with quote


"Mike B" wrote...
Quote:
Bjorn Abelli wrote:

public boolean equals(Object o)
{
try
{
CompareProducts c = (CompareProducts) o;
return true;
}
catch (ClassCastException cce)
{
return false;
}
}

And now I can implement the equals method as well.
It seems to me that it is merely checking the type
of object? Is that correct?

In my example it's correct, as I assume you don't intend to create different
versions of your Comparator. If that would be the case, you could make a
more elaborated equals to compare to some variants, but as the documentation
states, it's safe to even *exclude* equals in your Comparator.

http://tinyurl.com/4tspx

// Bjorn A



Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.