android - How can I get custom unmodified list after changing its list object data in java -


   // here have list 

// how can prevent list modified after object data changed want have unmodified original list without updating data. have used arraylist , collection.unmofifiedlist also

how can original list i.e.

"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 getting list 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());  } 

not without lot of work around. make student listenable:

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

and implement list listener disallows updates upon item changes:

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         }     }     // delegate other list methods in same way } 

i cannot see point in doing though, have "weird design" smell.


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -