aspose file tools*
The moose likes Beginning Java and the fly likes Comparing one array to another Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Comparing one array to another" Watch "Comparing one array to another" New topic
Author

Comparing one array to another

Corbin Blake
Greenhorn

Joined: Nov 10, 2004
Posts: 22
I am trying to sort an array and I am getting a < operator cannot be compared to error when i am trying to compare the positions in the array.

ex.
anArray[i] < anArray[minPos];

i = a position in the array
minPos = the minimum position in that array

Am i using the wrong operator or just using it incorrectly?
How would i do it?

If you need to see my code here it is.




I hope you know your paying me for this!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

You can't compare objects, including Strings or in this case Sequence objects, using < or any other operator besides == and != . You'll need a comparison function. For example, you might give Sequence a method like



Now instead of using "<", you can say

if (seqArray[i].compare(seqArray[minPos]) < 0)

I don't know if this is homework and you're obligated to write your own sort method, but note that the java.util.Arrays class has some lovely static sort() methods to sort arrays of many types. To sort an array of some user-defined type, you need to make that type implement the java.util.Comparable interface which has one method, compareTo(), which looks very much like the above compare() method. Note that String already implements Comparable.

There's actually another alternative to implementing Comparable -- you can instead create another class that implements Comparator and use an instance of that to direct the sorting; this is not any easier.


[Jess in Action][AskingGoodQuestions]
Corbin Blake
Greenhorn

Joined: Nov 10, 2004
Posts: 22
I think that i implemented what you suggested correctly, I created a method compareTo that looks like this.



I got the syntax from the API, for the compareTo method in java.lang.String,
but i keep getting a "cannot resolve symbol error char(int)" what am i doing wrong?

Appreciate the help
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Well, does Sequence have a charAt(int) method? I notice that the method uses charAt(), but the error message says char(). The latter would not be a legal method name, as it's a keyword (the name of a built-in type.)
[ December 08, 2004: Message edited by: Ernest Friedman-Hill ]
Corbin Blake
Greenhorn

Joined: Nov 10, 2004
Posts: 22
Okay, I came up with a better method to use selection sort but (funny, thing is) i am getting the same error. Ha!Ha!Ha! i am going to need a compareTo method, and i have been trying to write it, hopefully someone can walk me through it.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Comparing one array to another