• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Comparing one array to another

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Corbin Blake
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
reply
    Bookmark Topic Watch Topic
  • New Topic