Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 顺序搜索无法按预期工作_Java_Arraylist - Fatal编程技术网

Java 顺序搜索无法按预期工作

Java 顺序搜索无法按预期工作,java,arraylist,Java,Arraylist,我已经编写了一个程序,通过按下按钮,将用户输入的单词放入ArrayList。还有另一个文本字段,用户可以在其中输入字母或单词,用户可以通过另一个按钮在ArrayList中搜索。我正在使用顺序搜索算法来实现这一点,但它并没有像我期望的那样工作;如果找到了搜索的单词,搜索函数将返回,并在文本区域中打印出找到该单词的位置以及在数组中找到该单词的位置。这是可行的,但只适用于第一次搜索。如果找不到该单词,函数应打印出找不到该单词。这是我想要的 问题是,在我搜索了一个单词后,它显示了在ArrayList中可

我已经编写了一个程序,通过按下按钮,将用户输入的单词放入ArrayList。还有另一个文本字段,用户可以在其中输入字母或单词,用户可以通过另一个按钮在ArrayList中搜索。我正在使用顺序搜索算法来实现这一点,但它并没有像我期望的那样工作;如果找到了搜索的单词,搜索函数将返回,并在文本区域中打印出找到该单词的位置以及在数组中找到该单词的位置。这是可行的,但只适用于第一次搜索。如果找不到该单词,函数应打印出找不到该单词。这是我想要的

问题是,在我搜索了一个单词后,它显示了在ArrayList中可以找到这个单词的位置,然后我按下按钮时,无论输入的字母/单词是否在数组中,都不会发生任何事情。就像文本存储的字符串没有改变一样。我不明白为什么。。。下面是搜索功能的自定义
,然后是我的
类:

public class Search {
    static private int i;
    static String index;
    static boolean found = false;

public static String sequencial (ArrayList<String> list, String user) {
    for (int i = 0; i < list.size(); i++) {
        if (list.get(i).equals(user)) {
            index = "The word " + user  + " exist on the place " + i + " in the Arraylist";
            found = true;
        }
    }
    if (!found) {
        index = "The word " + user + " could not be found";
    }
    return index;
}
ArrayList<String> list = new ArrayList<String>();
ArrayList<String> s = new ArrayList<String>();   

private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {                                          
    // TODO add your handling code here:
    txtaOutput.setText("");
    String word = txtfAdd.getText();
    list.add(word);
    for (int i = 0; i < list.size(); i++) {
        txtaOutput.append("" + list.get(i) + "\n");
    }
}

private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {                                        
    // TODO add your handling code here:
    String user = txtfSearch.getText();
    txtaOutput.setText("");
    String index = Search.sequencial(list, user);
    txtaOutput.setText("" + index);
}

感谢您的帮助

问题是您将
found
变量声明为
static
。当找到第一个单词时,它被设置为
true
,并且没有任何东西会将其设置回false。与其将其作为静态变量,不如将其声明为
sequencial
(顺便说一句,它的拼写是顺序的)函数中的局部变量,就在for循环之前


事实上,所有声明为静态的变量都应该是局部变量。声明静态变量从来都不是一个好主意

问题是您将
found
变量声明为
static
。当找到第一个单词时,它被设置为
true
,并且没有任何东西会将其设置回false。与其将其作为静态变量,不如将其声明为
sequencial
(顺便说一句,它的拼写是顺序的)函数中的局部变量,就在for循环之前


事实上,所有声明为静态的变量都应该是局部变量。声明静态变量从来都不是一个好主意

正如其他用户所说:

  • List#indexOf(Object)
    方法。您应该使用它,而不是重新发明轮子(除非您需要,在这种情况下,您可以查看
    ArrayList
    实现)。还有其他的集合,比如
    HashSet
    ,它们更适合查找,但我想这是另一个历史
  • 变量(i、index、found)的范围和名称容易出错。其他方法甚至类是否需要访问这些变量?如果需要保留这些变量,可能需要选择可见性(public、protected、private)。“索引”是对消息名称的误导性选择
这将是您的代码的一个稍微简化/更正的版本:

// Ommit those unneeded static variables

public static String sequencial (ArrayList<String> list, String user) {
    int indexFound = list.indexOf(user);
    if (user >= 0) {
         return "The word " + user  + " exist on the place " + indexFound + " in the Arraylist";
    } else {
         return "The word " + user + " could not be found";
    }
}
...
private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {                                        
    String user = txtfSearch.getText();
    // txtaOutput.setText("");
    String seqMessage = sequencial(list, user);
    txtaOutput.setText(seqMessage);
}
//编辑那些不需要的静态变量
公共静态字符串序列(ArrayList,字符串用户){
int indexFound=list.indexOf(用户);
如果(用户>=0){
返回“单词”+user+“存在于Arraylist中的“+indexFound+”位置”;
}否则{
返回“找不到单词“+user+”;
}
}
...
private void btnSearchActionPerformed(java.awt.event.ActionEvent evt){
字符串user=txtfSearch.getText();
//txtaOutput.SETEXT(“”);
字符串seqMessage=顺序(列表,用户);
txtaOutput.setText(seqMessage);
}

正如其他用户所说:

  • List#indexOf(Object)
    方法。您应该使用它,而不是重新发明轮子(除非您需要,在这种情况下,您可以查看
    ArrayList
    实现)。还有其他的集合,比如
    HashSet
    ,它们更适合查找,但我想这是另一个历史
  • 变量(i、index、found)的范围和名称容易出错。其他方法甚至类是否需要访问这些变量?如果需要保留这些变量,可能需要选择可见性(public、protected、private)。“索引”是对消息名称的误导性选择
这将是您的代码的一个稍微简化/更正的版本:

// Ommit those unneeded static variables

public static String sequencial (ArrayList<String> list, String user) {
    int indexFound = list.indexOf(user);
    if (user >= 0) {
         return "The word " + user  + " exist on the place " + indexFound + " in the Arraylist";
    } else {
         return "The word " + user + " could not be found";
    }
}
...
private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {                                        
    String user = txtfSearch.getText();
    // txtaOutput.setText("");
    String seqMessage = sequencial(list, user);
    txtaOutput.setText(seqMessage);
}
//编辑那些不需要的静态变量
公共静态字符串序列(ArrayList,字符串用户){
int indexFound=list.indexOf(用户);
如果(用户>=0){
返回“单词”+user+“存在于Arraylist中的“+indexFound+”位置”;
}否则{
返回“找不到单词“+user+”;
}
}
...
private void btnSearchActionPerformed(java.awt.event.ActionEvent evt){
字符串user=txtfSearch.getText();
//txtaOutput.SETEXT(“”);
字符串seqMessage=顺序(列表,用户);
txtaOutput.setText(seqMessage);
}

当您想要使用常量时,我们使用静态属性。您不应该在此处使用静态属性。第一次更改查找到的属性时会出现问题,以后不会再更改。从那时起,它将永远是真实的。与索引属性类似。以下是您可以修复此问题的代码:

public class Search {

    public static SearchResult sequencial (ArrayList<String> list, String user) {
        SearchResult result = null;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).equals(user)) {
                String index = "The word " + user  + " exist on the place " + i + " in the Arraylist";
                boolean found = true;
                result = new SearchResult(index, found);
                break;
            }
        }

        if (result == null) {
            String index = "The word " + user + " could not be found";
            result = new SearchResult(index);
        }
        return result;
    }

    //sample inner class
    static class SearchResult {
        private String index;
        private boolean found;

        public SearchResult(String index) {
            this.index = index;
        }

        public SearchResult(String index, boolean found) {
            this.index = index;
            this.found = found;
        }

        public String getIndex() {
            return index;
        }
        public void setIndex(String index) {
            this.index = index;
        }
        public boolean isFound() {
            return found;
        }
        public void setFound(boolean found) {
            this.found = found;
        }
    }
}
公共类搜索{
公共静态搜索结果序列(ArrayList,字符串用户){
SearchResult=null;
对于(int i=0;i