2017-09-14 33 views
-2
// Here i have list 

//我怎樣才能阻止列表來修改它的對象數據改變 後,我想有一個未經修改的原始列表不更新它的數據。 我還使用ArrayList和Collection.unmofifiedList也我如何在Java中改變其列表對象數據後得到定製未修改列表

如何才能得到原始列表即

"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

是的我想要一個使用getter setter更改數據後無法修改的列表。 @ΦXocę웃Перепз –

回答

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

和實施清單​​是不允許在項目的變化更新的監聽器:

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 
} 

我真的不能看到這一點的,雖然這樣做,它確實有一個「古怪設計「的氣味。

0

你有不同的概念deppending上你所需要的:

  1. 您可以使用unmodifiableList這意味着這是一個你不能修改(您不能添加或刪除對象的列表那),但你可以肯定地獲得該名單上的那些對象的通過修改其字段改變其狀態......

  2. 可以定義類學生 inmutable所以你不能改變在創建對象後,其狀態......

那些是2個不同的策略和不不愛交際(你可以定義一個,兩個或沒有在你的架構。)

0

對於您需要做類(學生類)未經修改的或不變的,這樣你就不會允許修改的對象:

嘗試以下之一:

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) 

希望這有助於:)

+0

謝謝,它真的有幫助 –

+0

歡迎。請接受它,如果它解決了您的問題。 –