是否有一种方法可以在不删除现有记录的情况下更新Arraylist中的记录?



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

例如,我的记录有名字、姓氏、首字母、id等属性。是否有一种方法可以更新记录中的名字,而不必同时提供所有其他属性值?

目前我所做的是当用户给出一个id时,我发现它是否与数组中的任何记录匹配,如果它匹配,我将其从数组中删除,并使用户从一开始输入所有详细信息。

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

}
}

如果您的记录是一个对象,其中包含可变字段(getter和setter),如name…

根据某个标识符查找对象,并简单地用新值调用setter来替换旧值。

使用set()方法

Form Java API Docs:

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

如果你想更新一个或两个值,你可以使用setter。如果你知道当前对象的索引你就可以把新对象添加到那个索引如:- Arraylist。Add (index, element)这将更新现有的元素

你的对象应该包含设置/获取其属性的方法,无论是直接访问它们,还是通过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();
// 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;
    }
}

相关内容

  • 没有找到相关文章

最新更新