我想搜索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 :(得分:1)
首先,您的索引为0,因此您的for
不会循环播放。将索引替换为myBook.size()