1

这是我的申请

“Textform”,从搜索框中获取值。

“listKamus”,从数组中取值。然后“玩家名称”,将其值更改为字符串。

"KMP.knutMorris(textform, playerName)", 发送 textform, playerName 值到 knutMorris 类

主要课程

public void onTextChanged(CharSequence s, int arg1, int arg2, int arg3)
{
    String textform = s.toString();
    searchResults.clear();

    for(int i=0;i<listKamus.size();i++)
       {
      String playerName=listKamus.get(i).toString();
      KMP.knutMorris(textform, playerName);
        if(KMP.value==1){
        searchResults.add(listKamus.get(i));
        }
        }
    adapter.notifyDataSetChanged();
}

KMP 级

public class KMP {

/** Failure array **/

private int[] failure;
public static int value;

/** Constructor **/

public KMP(String text, String pat)

{

/** pre construct failure array for a pattern **/

failure = new int[pat.length()];

fail(pat);

/** find match **/

int pos = posMatch(text, pat);

if (pos >= 0)
{
KMP.value = 1;
}
}

/** Failure function for a pattern **/

private void fail(String pat)

{

int n = pat.length();

failure[0] = -1;

for (int j = 1; j < n; j++)

{

    int i = failure[j - 1];

    while ((pat.charAt(j) != pat.charAt(i + 1)) && i >= 0)

        i = failure[i];

    if (pat.charAt(j) == pat.charAt(i + 1))

        failure[j] = i + 1;

    else

        failure[j] = -1;

}

}

/** Function to find match for a pattern **/

private int posMatch(String text, String pat)

{

int i = 0, j = 0;

int lens = text.length();

int lenp = pat.length();

while (i < lens && j < lenp)

{

    if (text.charAt(i) == pat.charAt(j))

    {

        i++;

        j++;

    }

    else if (j == 0)

        i++;

    else

        j = failure[j - 1] + 1;

}

return ((j == lenp) ? (i - lenp) : -1);

}

/** Main Function **/

public static void knutMorris(String textform, String isidatabase)

{

String text = textform;

String pattern = isidatabase;

KMP kmp = new KMP(text, pattern);        

}

我希望人们在搜索框中键入时显示正确的数组列表。

我认为错误之一在这里

主要课程

 if(KMP.value==1){
        searchResults.add(listKamus.get(i));
        }

或者在这里

KMP 级

if (pos >= 0)
{
KMP.value = 1;
}

谁能告诉我如何解决这个问题?

4

0 回答 0