Java 使用后缀数组搜索后缀

Java 使用后缀数组搜索后缀,java,arraylist,comparator,binary-search,suffix-array,Java,Arraylist,Comparator,Binary Search,Suffix Array,我构造了一个后缀数组,它由ArrayList实现 我想使用此列表在后缀数组中搜索后缀。 为此,我对列表进行了排序并使用了二进制搜索,但“search”函数一直返回-1 我不知道我做错了什么。我已经重写了Hashcode和equals 我还更改了“equals”的默认定义,但仍然得到相同的输出 import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util

我构造了一个后缀数组,它由ArrayList实现

我想使用此列表在后缀数组中搜索后缀。 为此,我对列表进行了排序并使用了二进制搜索,但“search”函数一直返回-1

我不知道我做错了什么。我已经重写了Hashcode和equals

我还更改了“equals”的默认定义,但仍然得到相同的输出

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class SuffixArrayNaive   {


/**
 * This class represents the elements in the suffix array with their respective
 * locations
 * @author Aneesh
 *
 */
private class Elements implements Comparator<Elements>{
    String value;
    int position;

    public Elements() {
        // TODO Auto-generated constructor stub
    }
    public Elements(String value, int position) {
        //super();
        this.value = value;
        this.position = position;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + getOuterType().hashCode();
        result = prime * result + position;
        result = prime * result + ((value == null) ? 0 : value.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        Elements other = (Elements) obj;
        if (!getOuterType().equals(other.getOuterType()))
            return false;
        if (value == null) {
            if (other.value != null)
                return false;
        } else if (!value.equals(other.value))
            return false;
        return true;
    }

    private SuffixArrayNaive getOuterType() {
        return SuffixArrayNaive.this;
    }

    @Override
    public int compare(Elements o1, Elements o2) {
        // TODO Auto-generated method stub
        if (o1.value.compareTo(o2.value)>0){
            return 1;
        }
        if (o1.value.compareTo(o2.value)<0){
            return -1;
        }
        return 0;
    }
    @Override
    public String toString() {
        return "value=" + value + ", position=" + position + "\n";
    }
}

List<Elements> suffixArray = new ArrayList<>();

public static void main(String[] args) {
    // TODO Auto-generated method stub
    String baseString="banana";
    new SuffixArrayNaive().buildSuffixArray(baseString);
    String query="na";
    new SuffixArrayNaive().search(query);
}


private int search(String query) {
    // TODO Auto-generated method stub
    int result = -1;
    int res = Collections.binarySearch(suffixArray, new Elements(query, -1), new Elements());
    //printing -1 always!!
    //what is wrong?
    System.out.println(res);
    result = res;
    return result;
}

private void buildSuffixArray(String baseString) {
    // TODO Auto-generated method stub
    //generate all suffixes of the baseString
    int length= baseString.length();

    for (int i=0;i<length;++i){
        suffixArray.add(new Elements(baseString.substring(i), i));
    }

    Collections.sort(suffixArray, new Elements());
}
}
import java.util.ArrayList;
导入java.util.Collections;
导入java.util.Comparator;
导入java.util.List;
公共类后缀数组{
/**
*此类表示后缀数组中的元素及其各自的
*地点
*@作者Aneesh
*
*/
私有类元素实现了Comparator{
字符串值;
内部位置;
公共元素(){
//TODO自动生成的构造函数存根
}
公共元素(字符串值、int位置){
//超级();
这个值=值;
这个位置=位置;
}
@凌驾
公共int hashCode(){
最终整数素数=31;
int结果=1;
result=prime*result+getOuterType().hashCode();
结果=基本*结果+位置;
result=prime*result+((value==null)?0:value.hashCode();
返回结果;
}
@凌驾
公共布尔等于(对象obj){
if(this==obj)
返回true;
if(obj==null)
返回false;
元素其他=(元素)obj;
如果(!getOuterType().equals(other.getOuterType()))
返回false;
如果(值==null){
if(other.value!=null)
返回false;
}如果(!value.equals(other.value))
返回false;
返回true;
}
私有后缀arraynaive getOuterType(){
返回后缀arraynaive.this;
}
@凌驾
公共整数比较(元素o1、元素o2){
//TODO自动生成的方法存根
如果(o1.值与(o2.值)>0){
返回1;
}
如果(o1.value.compareTo)(o2.value)问题在这里:

new SuffixArrayNaive().buildSuffixArray(baseString);
String query="na";
new SuffixArrayNaive().search(query);
在SuffixArrayNaive的一个对象中,您正在创建后缀数组(通过填充列表),同时搜索另一个SuffixArrayNaive实例,数组(列表)为空

请记住,您的列表定义为非静态:

List<Elements> suffixArray = new ArrayList<>();

为什么是
super()
调用和无参数构造函数?eclipse自动生成了它。忽略它。我现在对它进行了注释。
super()
构造函数调用是隐式执行的(如果父类有可见的无参数构造函数)。此外,您不需要有两个变量
res
result
。您只需使用一个变量即可。@MinecraftShamrock-是的,我理解,我只是为了测试目的而这样做。谢谢,有时包含
main
方法的类实例会让人感到困惑。@almas我犯了一个巨大的错误。不管怎样,谢谢你的回答!
SuffixArrayNaive suffixArrayNaive = new SuffixArrayNaive();
suffixArrayNaive.buildSuffixArray(baseString);
String query="na";
suffixArrayNaive.search(query);