2017-01-29 62 views
1

大家好我是Java新手,仍然試圖讓我的頭在陣列上,任何幫助都非常感激。我的程序需要讀取一個CSV文件,如下面和刪除或一個人添加到ArrayList從訂單中刪除一個項目ArrayList

person, add, personid, firstname 
person, del, personid 

所以我試圖找出如何最好地去了解這個,基本上,我需要尋找一個有序陣列列出並從列表中刪除一個人。

來自主類的代碼。從第二類

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; 
    } 

} 

回答

2

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"); 

    } 
} 

代碼在arrayList你可以找到一個元素與

int index = MyArrayList.indexof(MyObject) 

的索引,然後你可以使用刪除

MyArrayList.remove(index) 

無論如何,我認爲你應該在構造函數中做

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

+0

爲什麼找到索引時,你可以使用'remove(Object o)' –

+0

這就是我想知道的,我必須作爲一個字符串傳遞值,所以在第二課我需要我的刪除方法來搜索ArrayList爲我傳入的字符串,並將其刪除,如果找到。我想我錯過了很多代碼來完成這個任務,但是...... – Newb2Java