Java 数组列表错误 ArrayList aList=new ArrayList(); 公众人士(){ String n=JOptionPane.showInputDialog(null,“请输入名称”); 字符串a=JOptionPane.showInputDialog(null,“请输入名称”); 字符串p=JOptionPane.showInputDialog(null,“请输入名称”); PersonInfo person=新PersonInfo(n,a,p); 添加(人); } 公共无效搜索(字符串n){ 对于(int i=0;i
显然您的列表是空的(您是否使用了Java 数组列表错误 ArrayList aList=new ArrayList(); 公众人士(){ String n=JOptionPane.showInputDialog(null,“请输入名称”); 字符串a=JOptionPane.showInputDialog(null,“请输入名称”); 字符串p=JOptionPane.showInputDialog(null,“请输入名称”); PersonInfo person=新PersonInfo(n,a,p); 添加(人); } 公共无效搜索(字符串n){ 对于(int i=0;i,java,arrays,arraylist,indexoutofboundsexception,Java,Arrays,Arraylist,Indexoutofboundsexception,显然您的列表是空的(您是否使用了AddPerson()函数?) 在您的for循环中有以下条件:i假设所有其他事情都正常,更改for循环中的条件。您已经编写了- ArrayList aList = new ArrayList(); public void AddPerson() { String n = JOptionPane.showInputDialog(null, "Please Enter Name"); String a = JOptionPane.showInputD
AddPerson()
函数?)
在您的
for循环中
有以下条件:i假设所有其他事情都正常,更改for循环中的条件。您已经编写了-
ArrayList aList = new ArrayList();
public void AddPerson() {
String n = JOptionPane.showInputDialog(null, "Please Enter Name");
String a = JOptionPane.showInputDialog(null, "Please Enter Name");
String p = JOptionPane.showInputDialog(null, "Please Enter Name");
PersonInfo person = new PersonInfo(n, a, p);
aList.add(person);
}
public void Search(String n) {
for (int i = 0; i <= aList.size(); i++) {
PersonInfo person = (PersonInfo) aList.get(i);
if (n.equals(person.name)) {
person.PrintInfo();
}
}
}
public void remove(String n) {
for (int i = 0; i <= aList.size(); i++) {
PersonInfo person = (PersonInfo) aList.get(i);
if (n.equals(person.name)) {
aList.remove(i);
}
}
}
}
for(int i=0;i要在添加/删除函数中遍历列表,请将终止条件更改为数组、ArrayList、列表从索引0开始
因此,如果上述任何一项的大小是5,那么它们项的索引是0到4
这里您要做的是,您正在尝试访问大小索引,这意味着,如果数组列表的大小为5,那么您正在尝试访问索引5,for(int i=0;i您的PersonInfo类是什么?从外观上看,也许您应该使用i
而不是我感谢Jo Liss爵士的作品all@Muhammad:)如果它解决了您的问题,请将其标记为答案problem@MuhammadZubairKHan:每次有人要求更改已接受的答案时(尤其是对于以后回答的相同答案),更改已接受的答案不是最佳做法。
for (int i = 0; i <= aList.size(); i++) {
}
for (int i = 0; i < aList.size(); i++) {
}
public void Search(String n) {
for (int i = 0; i <= aList.size(); i++) {
public void remove(String n) {
for (int i = 0; i <= aList.size(); i++) {
for (int i = 0; i < aList.size(); i++)