2017-05-26 113 views
0

我寫了這個代碼:改變一個對象的值,也改變另一個(相同類)對象的相同的值-Java

public class ArraLists { 
    public static void main(String[] args) { 
     ArrayList<Amphitheater> amphitheaters= new ArrayList<Amphitheater>(); 

      Amphitheater a1 = new Amphitheater("Amph 2"); 
      Amphitheater a2 = new Amphitheater("Amph 3"); 
      Amphitheater a3 = new Amphitheater("Amph 8"); 
      Amphitheater a4 = new Amphitheater("Amph 9"); 
      Amphitheater a5 = new Amphitheater("Amph 11"); 
      Amphitheater a6 = new Amphitheater("Amph 10"); 
      Amphitheater a7 = new Amphitheater("Amph 12"); 
      Amphitheater a8 = new Amphitheater("Amph 13"); 

      Course cC1 = new Course("Math","M123"); 

      amphitheaters.add(a1); 
      amphitheaters.add(a2); 
      amphitheaters.add(a3); 
      amphitheaters.add(a4); 
      amphitheaters.add(a5); 
      amphitheaters.add(a6); 
      amphitheaters.add(a7); 
      amphitheaters.add(a8); 

      DateExamination d1= new DateExamination("03 May",amphitheaters); 
      DateExamination d2= new DateExamination("04 May",amphitheaters); 

      ArrayList<Amphitheater> amph1= d1.getAmphitheaters(); 
      ArrayList<Amphitheater> amph2= d2.getAmphitheaters(); 

      amph2.get(1).addCourse(1,cC1); 

      System.out.println("Amphitheaters of D1"); 
      for(Amphitheater a: amph1) { 
       System.out.println(a.getName()); 
       System.out.println(a.getAvailableHours()); 
      } 

      System.out.println("Amphitheaters of D2"); 

      for(Amphitheater a: amph2) { 
       System.out.println(a.getName()); 
       System.out.println(a.getAvailableHours()); 
      } 

      System.out.println("\n\nList of Amphitheaters that I created in Main."); 

      for(Amphitheater a: amphitheaters) { 
       System.out.println(a.getName()); 
       System.out.println(a.getAvailableHours()); 
      } 
     } 
} 


public class Course { 

    private String name; 
    private String id; 

    private ArrayList<Amphitheater> amphitheaters= new ArrayList<Amphitheater>(); 
    private ArrayList<Integer> hours= new ArrayList<Integer>(); 

    public Course(String name, String id){ 
     this.name=name; 
     this.id=id; 
    } 

    public void addAmphitheater(Amphitheater amphitheater,int hour){ 
     amphitheaters.add(amphitheater); 
     hours.add(hour); 
    } 
} 




public class Amphitheater{ 

    private String name; 
    private HashMap<Integer,Boolean> availableHours=new HashMap<Integer,Boolean>(); //Hour,Available 
    private HashMap<Integer,Course> courses= new HashMap<Integer,Course>(); //Hour,Course 

    public Amphitheater(String name){ 
     this.name=name; 
     for(int i=0; i<5; i++){ 
      availableHours.put(i,true); 
      courses.put(i,null); 
     } 
    } 
    public boolean isHourAvailable(Integer hour){ 
     return availableHours.get(hour); 
    } 

    public ArrayList<Integer> getAvailableHours(){ 
     ArrayList<Integer> avHours= new ArrayList<Integer>(); 
     for(int i=0; i<5; i++){ 
      if(availableHours.get(i)) 
       avHours.add(i); 
      } 
      return avHours; 
     } 

    public void addCourse(Integer hour,Course course){ 
     courses.put(hour,course); 
     availableHours.put(hour, false); 
     course.addAmphitheater(this,hour); 

    } 

    public void setName(String name){ 
     this.name=name; 
    } 

    public String getName(){ 
     return name; 
    } 

} 






public class DateExamination{ 

    private ArrayList<Amphitheater> amphitheaters; 
    private String name; 

    public DateExamination(String name,ArrayList<Amphitheater> amphitheaters) { 
     this.name=name; 
     this.amphitheaters=amphitheaters; 
    } 

    public ArrayList<Amphitheater> getAmphitheaters(){ 
     return amphitheaters; 
    } 

} 

我作爲參數傳遞到DateExamination對象ArrayList與競技場。

的問題是,當我改變的值的d1DateExamination)的圓形劇場,自動相同的值被改變爲的d2DateExamination)的競技場。

此外,我在Main中創建的ArrayList已更改。我認爲所有的ArrayList都提到了相同的對象,但我真的不明白。

我希望DateExamination的每個對象有一個ArrayList圓形劇場,它將獨立於其他圓形劇場。

我也嘗試通過這個ArrayList作爲參數,但沒有結果。

+0

不知道這是否會算作重複問題,但請查看https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value – Zircon

+0

另請參閱[陷阱:在stackoverflow文檔中將變量看作對象](https://stackoverflow.com/documentation/java/4388/common-java-pitfalls/24130)。 –

回答

0

這是因爲它們都具有相同的對象,您需要改爲創建每個對象的新實例。

ArrayList<Amphitheater> amphitheaters= new ArrayList<>(); 
    ArrayList<Amphitheater> amphitheaters1= new ArrayList<>(); 

    Course cC1 = new Course("Math","M123"); 

    amphitheaters.add(new Amphitheater("Amph 2")); 
    amphitheaters.add(new Amphitheater("Amph 3")); 
    amphitheaters.add(new Amphitheater("Amph 8")); 
    amphitheaters.add(new Amphitheater("Amph 9")); 
    amphitheaters.add(new Amphitheater("Amph 11")); 
    amphitheaters.add(new Amphitheater("Amph 10")); 
    amphitheaters.add(new Amphitheater("Amph 12")); 
    amphitheaters.add(new Amphitheater("Amph 13")); 

    amphitheaters1.add(new Amphitheater("Amph 2")); 
    amphitheaters1.add(new Amphitheater("Amph 3")); 
    amphitheaters1.add(new Amphitheater("Amph 8")); 
    amphitheaters1.add(new Amphitheater("Amph 9")); 
    amphitheaters1.add(new Amphitheater("Amph 11")); 
    amphitheaters1.add(new Amphitheater("Amph 10")); 
    amphitheaters1.add(new Amphitheater("Amph 12")); 
    amphitheaters1.add(new Amphitheater("Amph 13")); 



    DateExamination d1= new DateExamination("03 May", new ArrayList<>(amphitheaters)); 
    DateExamination d2= new DateExamination("04 May", new ArrayList<>(amphitheaters1)); 

這是一個骯髒的黑客來解決它,但它會適用於你的情況,如果那只是你需要這一次。

+0

這不僅是一次。我希望每次用戶想要「創建」一個對象DateExamination時,都會自動獲得這個ArrayList的這個對象。爲了簡單起見,我是這樣寫的。無論如何,謝謝! – OrestisNer

相關問題