有没有办法更新Arraylist中的记录而不删除现有记录?

时间:2013-05-02 12:00:28

标签: java arraylist record updates

我有一个arraylist,其中记录存储为对象。我想知道是否有办法更新数组列表中的记录而不删除现有记录?

例如,我的记录包含名字,姓氏,姓名缩写,ID等属性。有没有办法更新记录中的名字,而不必同时提供所有其他属性值?

目前我所做的是当用户提供id时,我发现id是否匹配数组中的任何记录,如果匹配,我将其从数组中删除并让用户从头开始输入所有细节。

6 个答案:

答案 0 :(得分: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);
    }


}

}

答案 1 :(得分:0)

如果你的记录是一个包含可变字段(getter和setter)的对象,就像名字一样......用某个标识符找到对象,只需用新值调用setter来替换旧值。

答案 2 :(得分: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

答案 3 :(得分:0)

如果要更新一个或两个值,可以使用setter。如果您知道当前对象的索引,则可以将新对象添加到该索引 例如: - Arraylist.add(index,element)这将更新现有元素。

答案 4 :(得分:0)

你的对象应该包括一种设置/获取属性的方法,可以通过直接访问它们,或通过set / 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();

答案 5 :(得分: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;
    }
}