Java - 尝试搜索 ArrayList 时 For 循环未执行
这是我关于堆栈溢出的第一个问题,但我在 Java 方面有一些经验。我现在正在制作一个 Java 应用程序(575 行并且还在增加!),并尝试在 ArrayList 中搜索字符串。但是我不希望它是准确的!让我澄清一下:我想迭代每个 ArrayList 元素并在该字符串中搜索另一个字符串。如果在 ArrayList 元素中找到该字符串,(目前)我希望将其打印到控制台。我希望我已经说得足够清楚了。
以下是相关代码。所有变量均已定义并且代码已编译,只是不打印任何输出(来自搜索函数)。我很确定这是因为 for 循环没有执行,但我很困惑为什么。
//the keylistener that calls the search() function, attached to a JTextField that the query is entered into
class searchFieldListener implements KeyListener {
searchFieldListener() {
}
public void keyTyped(KeyEvent event) {
if (event.getID() == KeyEvent.KEY_TYPED) {
query = searchField.getText()+Character.toString(event.getKeyChar());
System.out.println(query);
for (i = 0; i == nameList.size(); i++) {
search(query, i);
}
}
}
public void keyReleased(KeyEvent event) {
}
public void keyPressed(KeyEvent event) {
}
}
//the troublesome search() function
void search(String query, int iter) {
searchString = nameList.get(iter);
System.out.println(searchString);
if (searchString.indexOf(query) != -1) {
System.out.println(Integer.toString(iter));
} else {
System.out.println("not found \n");
}
}
变量/对象和用途:
- searchFieldListener
出于显而易见的原因,JTextField 的 KeyListener 称为 searchField。 - 查询
要搜索的文本字符串。 - 我
为什么每个人都在循环中使用 i ?我想这是一种编码传统。 - 姓名列表
名称的 ArrayList(嗯,废话)。 - 搜索字符串
要搜索的字符串(例如,尝试在 searchString 中查找 query)。 - iter
for 循环到目前为止已经执行的迭代次数。
<小时> 我再次希望我已经说得足够清楚了。谢谢!
this is my first question on stack overflow but I have some experience in Java. I am making a Java application at the moment (575 lines and counting!) and am trying to search through an ArrayList for a string. But I do not want it to be exact! Let me clarify: I want to iterate through each ArrayList element and search that string for another string. if the string is found in the ArrayList element, (for now) I want it printed to the console. I hope I have been clear enough.
Following is the relevant code. All variables are defined and the code compiles, just no output (from the search function) is printed. I am pretty sure that it is because the for loop doesn't execute, but I am puzzled as to why.
//the keylistener that calls the search() function, attached to a JTextField that the query is entered into
class searchFieldListener implements KeyListener {
searchFieldListener() {
}
public void keyTyped(KeyEvent event) {
if (event.getID() == KeyEvent.KEY_TYPED) {
query = searchField.getText()+Character.toString(event.getKeyChar());
System.out.println(query);
for (i = 0; i == nameList.size(); i++) {
search(query, i);
}
}
}
public void keyReleased(KeyEvent event) {
}
public void keyPressed(KeyEvent event) {
}
}
//the troublesome search() function
void search(String query, int iter) {
searchString = nameList.get(iter);
System.out.println(searchString);
if (searchString.indexOf(query) != -1) {
System.out.println(Integer.toString(iter));
} else {
System.out.println("not found \n");
}
}
Variables/Objects and uses:
- searchFieldListener
The KeyListener for the JTextField called searchField for obvious reasons. - query
The string of the text to be searched for. - i
Why does everyone use i in loops? I guess it's a coding tradition. - nameList
The ArrayList of names (well, duh). - searchString
The string to be searched in (as in, try to find query in searchString). - iter
The number of iterations the for loop has been through so far.
Once again I hope I have been clear enough. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
for 循环未执行的原因是循环中使用的条件:
由于 size ArrayList 类的方法返回您可能想要拥有的元素数量
<代码>我< nameList.size() 代替。
The reason why your for loop is not executing is because of the condition used in the loop:
Since the size method of the ArrayList class returns the number of elements you might want to have
i < nameList.size()
instead.应该
不确定您是否需要<或 <= 不过,您只需根据需要修改它即可。
should be
Not sure if you need < or <= though, you just modify it to you needs.
你的 for 循环中有一个拼写错误。不应该这样:
看起来像这样:
have have a typo in your for-loop. shouldn't this:
look like this:
有几个正确答案,但缺少一个方面:
这是一个老式循环。从 Java 1.5 开始,您应该使用此习惯用法来迭代数组或可迭代对象(List 是 Iterable):
这种更简单的语法 a) 更不容易出错 b) 是迭代许多不同数据结构的常用方法,包括数组和集合。
在内部,这是此代码的快捷方式:
Several correct answers, but one aspect is missing:
This is an old-school loop. Starting from Java 1.5, you should use this idiom to iterate over an array or iterable (a List is an Iterable):
This simpler syntax is a) much less error-prone and b) a common way to iterate over many different data structures, including arrays and collections.
Internally this is a shortcut for this code: