[英]Removing an item from an order ArrayList
大家好,我是Java的新手,仍然想尽一切办法使用数组,非常感谢您的帮助。 我的程序需要读取一个CSV文件,例如下面的文件,然后删除一个人或将一个人添加到ArrayList中
person, add, personid, firstname
person, del, personid
因此,我试图找出最好的方法,基本上我需要搜索一个有序的数组列表,然后从列表中删除该人。
主类的代码。
public static void processPersonDeletion(String[]theLines){
Person personDel = new Person();
setPersonAttributes(personDel, theLines);
if(!personDel.equals(theLines)){
System.out.println("Person with license " + theLines[2] + " has "
+ "been removed from the log \nAll persons "
+ "properties will will also be removed from their "
+ "property log");
}
}
第二类代码
public class PersonLogImpl {
private boolean remove;
private boolean isPersonIdUnique;
private boolean add;
private ArrayList<Person> person = new ArrayList<>();
public ArrayList<Person> getPersonLog(){
return person;
}
public boolean add(person obj){ //add person object to ordered list
person.add(obj);
return add;
}
public boolean remove (String license){ //remove Person with specific license from list
person.remove(license); // and return true if successful
return remove;
}
// test if person with specific personid exists in log
public boolean isPersonIdUnique(String license){
isLicenseUnique = true;
return isLicenseUnique;
}
}
在arrayList
您可以找到具有
int index = MyArrayList.indexof(MyObject)
然后您可以使用删除它
MyArrayList.remove(index)
无论如何,我认为你应该做
ArrayList<Person> person = new ArrayList<Person>();
在构造函数中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.