我想搜索arraylist内的对象,从用户输入中获取值并将其打印到文本区域。这是代码。
//the arrayList i declared
Book[]myBook = new Book [30];
int index = 0;
private void searchBtnActionPerformed(java.awt.event.ActionEvent evt) {
String title = titleTF.getText();
boolean found = false;
for (int i = 0; i < index; i++) {
if (myBook[i].getTitle().equals(title));
{
outputTA.append("Book Title : " + myBook[i].getTitle() + "\n");
outputTA.append("Book Author : " + myBook[i].getAuthor() + "\n");
outputTA.append("Year of Publication : " + myBook[i].getYear() + "\n");
outputTA.append("Book Status : " + myBook[i].getStatus() + "\n");
outputTA.append("======================================\n");
found = true;
break;
}
}
if (found == false) {
JOptionPane.showMessageDialog(this, "Book is not Found! Please Try again!");
}
}
问题是,当我单击搜索按钮时,它将在arraylist中显示第一个对象。有人可以帮我解决,哪一行代码是错误的?谢谢。
最佳答案
首先,您的索引为0,因此您的for
不会循环。用myBook.size()
替换索引