// Here i have list
//如何在对象数据更改后阻止修改列表 我希望有一个未经修改的原始列表而不更新它的数据。 我还使用了Arraylist和Collection.unmofifiedList
我如何获得原始列表,即
"S Age"- 10
"S Age"- ABC
"S Name "- 30
"S Name "- XYZ
Student s1 = new Student();
s1.setAge(10);
s1.setName("ABC");
Student s2 = new Student();
s2.setAge(30);
s2.setName("XYZ");
ArrayList<Student> al = new ArrayList<Student>();
al.add(s1);
al.add(s2);
// here i am getting list all data
for(int i=0; i<al.size(); i++) {
System.out.println("F Age " + al.get(i).getAge());
System.out.println("F Name " + al.get(i).getName());
}
Student s3 = new Student();
s3 = al.get(0);
s3.setAge(50);
s3.setName("Shyam");
for(int i=0;i< al.size(); i++) {
System.out.println("S Age " + al.get(i).getAge());
System.out.println("S Name " + al.get(i).getName());
}
答案 0 :(得分:0)
不是没有很多工作。 让学生可以听:
interface Listener {
public void changed();
}
class Student {
private List<Listener> listeners = new ArrayList<>();
public void addListener(Listener l) { listeners.add(l); }
public void setAge(int age) {
//...
listeners.forEach(Listener::changed);
}
并将List实现为一个不允许在项目更改时更新的侦听器:
class MyList<T> implements List<T>, Listener {
private List<T> delegate = new ArrayList<T>();
private boolean editable = true;
public void changed() { editable = false; }
public void add(T item) {
if (editable) {
delegate.add(item);
} else {
throw new IllegalStateException(); // or something
}
}
// also delegate all other List methods in the same way
}
我无法真正看到这样做的重点,它确实有一种“怪异的设计”气味。
答案 1 :(得分:0)
你必须根据自己的需要改变不同的概念:
您可以使用 unmodifiableList ,这意味着这是一个您无法修改的列表(您无法添加或删除该对象)但您可以确保通过修改其字段来改变其状态......
您可以定义类学生 inmutable,这样您就无法在创建对象后更改其状态...
这些是两种不同的策略,并且不是分离的(您可以在架构中定义一个,两个或不定义。)
答案 2 :(得分:0)
为此,您需要使类(Student类)不可修改或不可变,以便您不允许修改对象:
尝试下面的一个:
public final class Student {
final String name;
final int age;
public Student(String name,int age)
{
this.name=name;
this.age =age;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public static void main(String[] args) {
Student s1 = new Student("ABC", 10);
/*s1.setAge(10);
s1.setName("ABC");*/
Student s2 = new Student("XYZ", 30);
/*s2.setAge(30);
s2.setName("XYZ");*/
ArrayList<Student> al = new ArrayList<Student>();
al.add(s1);
al.add(s2);
// here i am getting list all data
for (int i = 0; i < al.size(); i++) {
System.out.println("F Age " + al.get(i).getAge());
System.out.println("F Name " + al.get(i).getName());
}
Student s3 = new Student("sss",22);
s3 = al.get(0);
// Can't modify because it is not allowed
/*s3.setAge(50);
s3.setName("Shyam");*/
for (int i = 0; i < al.size(); i++) {
System.out.println("S Age " + al.get(i).getAge());
System.out.println("S Name " + al.get(i).getName());
}
}
}
创建不可变类的步骤:
• Class must be declared as final (So that child classes can’t be created)
• Data members in the class must be declared as final (So that we can’t change the value of it after object creation)
• A parameterized constructor
• Getter method for all the variables in it
• No setters(To not have option to change the value of the instance variable)
希望这会有所帮助:)
答案 3 :(得分:-1)
//Very nice question and are very complex but i am able to give answer. please use Map istean of array list because arraylist save Object reference
public static void main(String args[]){
int age = 20;
String name = "DS";
Student s1 = new Student();
s1.setAge(age);
s1.setName(name);
Student s2 = new Student();
s2.setAge(30);
s2.setName("PS");
//List<Student> list=new ArrayList<Student>();
Map<Integer,Student> list=new HashMap<Integer,Student>();
list.put(1,s1);
list.put(2,s2);
Student b =null;
for(Map.Entry<Integer, Student> entry:list.entrySet()){
int key=entry.getKey();
b=entry.getValue();
System.out.println(key+" Details:");
System.out.println(b.getAge()+" "+b.getName());
}
Student s3 = new Student();
int a = b.getAge();
String a1 = b.getName();
a = 100;
a1= "Shyamji";
for(Map.Entry<Integer, Student> entry:list.entrySet()){
int key=entry.getKey();
Student b1=entry.getValue();
System.out.println(key+" Details:");
System.out.println(b1.getAge()+" "+b1.getName());
}
}