Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
更改一个对象的值,同时更改另一个(同一类)对象的相同值-Java_Java_Arraylist - Fatal编程技术网

更改一个对象的值,同时更改另一个(同一类)对象的相同值-Java

更改一个对象的值,同时更改另一个(同一类)对象的相同值-Java,java,arraylist,Java,Arraylist,我编写了以下代码: public class ArraLists { public static void main(String[] args) { ArrayList<Amphitheater> amphitheaters= new ArrayList<Amphitheater>(); Amphitheater a1 = new Amphitheater("Amph 2"); Amphithea

我编写了以下代码:

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

}
公共类列表{
公共静态void main(字符串[]args){
ArrayList圆形剧场=新建ArrayList();
竞技场a1=新竞技场(“竞技场2”);
竞技场a2=新竞技场(“竞技场3”);
竞技场a3=新竞技场(“竞技场8”);
竞技场a4=新竞技场(“竞技场9”);
圆形剧场a5=新圆形剧场(“圆形剧场11”);
圆形剧场a6=新圆形剧场(“圆形剧场10”);
圆形剧场a7=新圆形剧场(“圆形剧场12”);
竞技场a8=新竞技场(“竞技场13”);
课程cC1=新课程(“数学”、“M123”);
圆形剧场.增加(a1);
圆形剧场。增加(a2);
圆形剧场.增加(a3);
圆形剧场.增加(a4);
圆形剧场。增加(a5);
圆形剧场。增加(a6);
圆形剧场.增加(a7);
圆形剧场.增加(a8);
日期检查d1=新日期检查(“5月3日”,圆形剧场);
日期检查d2=新日期检查(“5月4日”,圆形剧场);
ArrayList Amphith1=d1.getArrayList Amphitheaters();
ArrayList Amphith2=d2.getArrayList Amphitheaters();
2.get(1)addCourse(1,cC1);
System.out.println(“D1的圆形剧场”);
用于(圆形剧场a:圆形剧场1){
System.out.println(a.getName());
System.out.println(a.getAvailableHours());
}
System.out.println(“D2的圆形剧场”);
用于(圆形剧场a:圆形剧场2){
System.out.println(a.getName());
System.out.println(a.getAvailableHours());
}
System.out.println(“\n\n我在Main中创建的圆形剧场列表”);
用于(圆形剧场a:圆形剧场){
System.out.println(a.getName());
System.out.println(a.getAvailableHours());
}   
}
}
公共课{
私有字符串名称;
私有字符串id;
私人ArrayList圆形剧场=新建ArrayList();
private ArrayList小时数=新建ArrayList();
公共课程(字符串名称、字符串id){
this.name=name;
这个.id=id;
}
公共竞技场(竞技场竞技场,整小时){
圆形剧场。添加(圆形剧场);
小时。添加(小时);
}
}
公共级圆形剧场{
私有字符串名称;
private HashMap availableHours=new HashMap();//小时,可用
私有HashMap课程=新建HashMap();//小时,课程
公共圆形剧场(字符串名称){
this.name=name;

对于(int i=0;i这是因为它们都有相同的对象,因此需要为每个对象创建一个新实例

    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));
ArrayList圆形剧场=新建ArrayList();
ArrayList圆形剧场1=新ArrayList();
课程cC1=新课程(“数学”、“M123”);
竞技场。添加(新竞技场(“竞技场2”);
竞技场。添加(新竞技场(“竞技场3”);
竞技场。添加(新竞技场(“竞技场8”);
竞技场。添加(新竞技场(“竞技场9”);
竞技场。增加(新竞技场(“竞技场11”);
竞技场。添加(新竞技场(“竞技场10”);
竞技场。添加(新竞技场(“竞技场12”);
竞技场。增加(新竞技场(“竞技场13”);
竞技场1.增加(新竞技场(“竞技场2”);
竞技场1.增加(新竞技场(“竞技场3”);
竞技场1.增加(新竞技场(“竞技场8”);
竞技场1.增加(新竞技场(“竞技场9”);
竞技场1.增加(新竞技场(“竞技场11”);
竞技场1.增加(新竞技场(“竞技场10”);
竞技场1.增加(新竞技场(“竞技场12”);
竞技场1.增加(新竞技场(“竞技场13”);
日期检查d1=新日期检查(“5月3日”,新ArrayList(圆形剧场));
日期检查d2=新日期检查(“5月4日”,新ArrayList(圆形剧场1));

这是一个肮脏的黑客绕过它,但它将为您的情况下,如果这只是你需要这一次。

不确定这是否会被视为一个重复的问题,但请查看查看stackoverflow文档。这不仅是一次。我希望每一次用户想要“创建”一个对象日期检查,这个对象自动得到这个数组列表。为了简单起见,我这样写的。无论如何,谢谢!