2013-05-02 115 views
0

我有一個arraylist,其中記錄存儲爲對象。我想知道是否有方法來更新數組列表中的記錄而不刪除現有記錄?有沒有辦法更新Arraylist中的記錄而不刪除現有記錄?

例如,我的記錄有像名字,姓氏,首字母縮寫,身份證等屬性有沒有辦法更新記錄中的第一個名字,而不是必須給所有其他的屬性值?

目前我所做的是當用戶給出一個id,我發現它是否與數組中的任何記錄相匹配,如果是這樣,我將它從數組中刪除,並使用戶輸入所有來自開始。

+0

我不認爲這是否會工作,但你給一個嘗試.. 可以使用array.get(指數)來獲取對象,然後用改變它,商店名稱它在相同的指數 – Elior 2013-05-02 12:06:22

回答

2

ArrayList中存儲參考很好的做法,不會複製/創建新的對象。如果您更改存儲的對象引用,它也會反映在arrayList中。下面是一個示例代碼來演示:

package arraylistExample; 

import java.util.ArrayList; 

/** 
* Class represeting entity to be stored in Arraylist 
* 
*/ 
class Person { 

private String name; 
private int age; 
private String address; 

public Person(String name, int age, String address) { 
    super(); 
    this.name = name; 
    this.age = age; 
    this.address = address; 
} 

public String getName() { 
    return name; 
} 
public void setName(String name) { 
    this.name = name; 
} 
public int getAge() { 
    return age; 
} 
public void setAge(int age) { 
    this.age = age; 
} 
public String getAddress() { 
    return address; 
} 
public void setAddress(String address) { 
    this.address = address; 
} 

@Override 
public String toString() { 
    return "Person [name=" + name + ", age=" + age + ", address=" + address 
      + "]"; 
} 


} 

/** 
* Public class to run the demo 
* 
*/ 
public class ArraylistObjectModify { 

public static void main(String args[]) { 

    // Add an arraylist and add elements to it 
    ArrayList<Person> personList = new ArrayList<Person>(); 
    personList.add(new Person("Juned",32,"Bangalore")); 
    personList.add(new Person("Ahsan",31,"Delhi")); 
    personList.add(new Person("Sniper",1,"Grave")); 

    //Print list elements before change 
    System.out.println("Arraylist pre objects modification"); 
    System.out.println("----------------------------------"); 
    for(Person person:personList) { 
     System.out.println(person); 
    } 

    for(Person person:personList) { 
     if(person.getName().equals("Juned")) { 
      person.setName("ChangedJuned"); 
      person.setAddress("Hola-lulu"); 
     } 
    } 

    //Print list elements after change 
    System.out.println("Arraylist post objects modification"); 
    System.out.println("----------------------------------"); 
    for(Person person:personList) { 
     System.out.println(person); 
    } 


} 

} 
0

如果記錄是包含可變的字段(getter和setter)像名稱的對象...查找一些標識的對象,調用的setter新值,以取代舊的。

0

使用set()方法。

形式的Java API文檔:

set 
public E set(int index, 
      E element) 
Replaces the element at the specified position in this list with the specified element. 

here服用。

0

如果你想更新一個或兩個值,你可以使用setter。如果您知道當前對象的索引,則可以將新對象添加到該索引 例如: - Arraylist.add(index,element)將更新現有元素。

0

你的對象應該包括一種方法來設置/獲取它們的屬性,通過直接訪問他們是這樣,也可以通過設置/ get方法。

例如

ArrayList<YourObject> Records = new ArrayList<YourObject>(); 

//Loop through your ArrayList and check if their ID attribute matches 
for(YourObject record : Records) { 
    if(record.id == userGivenID) { 
     //prompt the user to change whichever values you want 
     Scanner s = new Scanner(System.in); 
     System.out.print("Change the name of this record > "); 
     record.setName(s.nextLine()); 
     ...etc... 
    } 
} 

它是使用get/set方法,如

record.setName("Bob"); 
String name = record.getName(); 
0
// Check this example 

public class Test { 
    public static void main(String[] args){ 
     List<Student> al = new ArrayList<Student>(); 

     Student s1 = new Student(1, "John", "Nash", "N"); 
     Student s2 = new Student(2, "John", "Slash", "s"); 

     al.add(s1); 
     al.add(s2); 


     for(Student s:al){ 
      if(s.getId() == 2){ 
       s.setfNmae("Nks"); 
       al.add(al.indexOf(s), s); 
      } 
      s.display(); 
     } 

    } 
} 

class Student{ 
    private int id; 
    private String fName; 
    private String lName; 
    private String initial; 

    Student(int id, String fName, String lName, String initial){ 
     this.id = id; 
     this.fName = fName; 
     this.lName = lName; 
     this.initial = initial; 
    } 
    void display(){ 
     System.out.println(id); 
     System.out.println(fName); 
     System.out.println(lName); 
     System.out.println(initial); 
    } 

    /** 
    * @return the id 
    */ 
    public int getId() { 
     return id; 
    } 

    /** 
    * @param id the id to set 
    */ 
    public void setId(int id) { 
     this.id = id; 
    } 

    /** 
    * @return the fNmae 
    */ 
    public String getfNmae() { 
     return fName; 
    } 

    /** 
    * @param fNmae the fNmae to set 
    */ 
    public void setfNmae(String fNmae) { 
     this.fName = fNmae; 
    } 

    /** 
    * @return the lName 
    */ 
    public String getlName() { 
     return lName; 
    } 

    /** 
    * @param lName the lName to set 
    */ 
    public void setlName(String lName) { 
     this.lName = lName; 
    } 

    /** 
    * @return the initial 
    */ 
    public String getInitial() { 
     return initial; 
    } 

    /** 
    * @param initial the initial to set 
    */ 
    public void setInitial(String initial) { 
     this.initial = initial; 
    } 
} 
+0

謝謝,我得到它使用get/set方法。謝謝大家。 – 2013-05-02 12:51:25