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
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