binarysearch problem with object arraylist

R

Rick D.

Hi all,

I have been trying to do a binary search on an arraylist containing
objects for several hours now, but for some reason it just won't work.
It's probably a simple mistake, but right now i don't see it. If
someone could take a look at my code and tell me what i'm doing wrong,
and how to fix it, i would really appreciate it. Or is this one of the
things that is just not possible in java?

I get this error:
----------------------------------------------------------
Main.java:21: cannot resolve symbol
symbol : method binarySearch (java.util.List,long)
location: class java.util.Collections
index = Collections.binarySearch(patterns,searchpattern);
^
1 error
----------------------------------------------------------

Here is my code:

----------------------------- start Main.java
import java.io.*;
import java.util.*;

public class Main
{

public static void main (String args[])
{
int index;
long searchpattern;

List patterns = new ArrayList();

patterns.add( new PatternNode(1) );
patterns.add( new PatternNode(3) );
patterns.add( new PatternNode(2) );

searchpattern=2;
Collections.sort(patterns);
index = Collections.binarySearch(patterns,searchpattern);
}

}
----------------------------- end Main.java


----------------------------- start PatternNode.java
import java.util.*;

public class PatternNode implements Comparable
{
public int count;
public long pattern;

public PatternNode ( long l)
{
pattern=l;
}

public int compareTo(Object obj)
{
PatternNode tmp = (PatternNode)obj;
if(this.pattern < tmp.pattern)
{
/* instance lt received */
return -1;
}
else if(this.pattern > tmp.pattern)
{
/* instance gt received */
return 1;
}
/* instance == received */
return 0;
}

}
----------------------------- end PatternNode.java

Best regards,
Rick
 
B

Bjorn Abelli

...
I have been trying to do a binary search on an arraylist containing
objects for several hours now, but for some reason it just won't work.
It's probably a simple mistake, but right now i don't see it. If
someone could take a look at my code and tell me what i'm doing wrong,
and how to fix it, i would really appreciate it. Or is this one of the
things that is just not possible in java?

Please don't multipost!

You can read my answer in comp.lang.java.help, which is the more appropriate
group for newbie-questions.

// Bjorn A
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top