We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Example using Collections
Page Index Toggle Pages: 1
Example using Collections? (Read 951 times)
Example using Collections?
Apr 15th, 2007, 2:18am
 
(This is for OSX 10.4.9 on a MacBook, processing 0124)

Anyone have a good example of using a List with a custom Comparator?  I've been trying various examples from the java docs and am getting some really strange error messages.  One was to the effect of "-source 1.5" or greater is required, so I'm wondering if they're implemented properly on the Mac or if there's no direct way to get to Comparators from within processsing.

thx,
--jet
Re: Example using Collections?
Reply #1 - Apr 15th, 2007, 10:40am
 
Processing is currently not supporting java 5 (1.5), see here. you should use java 1.4.x syntax to be safe.

what are you trying to do ... most of the times there are simpler (and faster) ways to do sorting than using javas builtin comparators.

anyways, here's a hybrid example showing both comparable and comparator:

Code:

ArrayList myList;

void setup()
{
myList = new ArrayList();
myList.add( new myObject(-1000) );
myList.add( new myObject(2000) );
myList.add( new myObject(1) );

Object[] unsorted = myList.toArray();
Object[] sorted = myList.toArray();

Arrays.sort(sorted, new myObjectComparator());

// or do instead:
// Arrays.sort(sorted);
// because the comparable interface is implemented in myObject

println( "unsorted \t sorted" );

for (int i=0; i < unsorted.length; i++)
{
println( ((myObject)unsorted[i]).anInt + " \t " + ((myObject)sorted[i]).anInt );
}
}

class myObject
implements Comparable
{
int anInt;

myObject ( int _i )
{
anInt = _i;
}

// you can as well use the comparable interface to compare objects.
// with this there's no need for another comparator class ...
int compareTo(Object other) throws ClassCastException
{
myObject mo = (myObject)other;
if ( anInt > mo.anInt ) return 1;
else if ( anInt < mo.anInt ) return -1;
return 0;
}
}

class myObjectComparator
implements Comparator
{
int compare(Object o1, Object o2)
{
myObject mo1 = (myObject)o1;
myObject mo2 = (myObject)o2;
if ( mo1.anInt > mo2.anInt ) return 1;
else if ( mo1.anInt < mo2.anInt ) return -1;
return 0;
}
}


F
Re: Example using Collections?
Reply #2 - Apr 15th, 2007, 3:48pm
 
Gah, I thought I was reading 1.4 docs, maybe that's part of the problem.  Looks like I was trying to use something from 1.5, your example is a bit different than what I was trying:

Comparator<FooObject> FOO_ALPHA =                                 new Comparator<FooObject>() {
       public int compare(FooObject f1, FooObject af2) {
           return f1.alphaString().compareTo(f2.alphaString());
       }

Anyway, I was just looking to order a list of objects by different fields within each object.  I'm relatively new to Java and trying to actually learn the API rather than write-C-in-Java.

thx,
Re: Example using Collections?
Reply #3 - Apr 15th, 2007, 6:41pm
 
Anything you read which has "foo<thing>" is java 1.5 at a minimum.
Re: Example using Collections?
Reply #4 - Apr 17th, 2007, 7:44pm
 
That code did the trick, thanks for the help!
--jet
Page Index Toggle Pages: 1