从订单ArrayList中删除项目

时间:2017-01-29 11:46:18

标签: java oop 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;
   }

}

1 个答案:

答案 0 :(得分:2)

arrayList中,您可以找到带

的元素的索引
int index = MyArrayList.indexof(MyObject)

然后你可以使用

删除它
MyArrayList.remove(index)

无论如何,我认为你应该这样做

ArrayList<Person> person = new ArrayList<Person>();

在构造函数中。