我正在更改对象的一部分但它不断更改列表中的所有对象而不考虑条件集

时间:2017-01-02 02:58:07

标签: java arrays arraylist lambda

我正在尝试将每个CourseGroup的每个星期一的句号1设置为true,但是对象会改变所有日期而不是仅仅星期一。请查看下面的示例

import java.util.ArrayList;

import java.util.Arrays;

import java.util.List;

import java.util.Objects;

class HelloWorld {



    public static void main(String[] args) {
        SchoolObject schoolObject;
        List<LecturePeriod> lecturePeriods = new ArrayList<>();
        for (int i = 1; i <= 10; i++) {
            String periodName = "Period " + i;
            int periodNo = i;
            boolean isPeriodAllocated = false;
            lecturePeriods.add(new LecturePeriod(periodName, periodNo, isPeriodAllocated));
        }

        List<Day> days = new ArrayList<>();
        for (int i = 1; i <= 5; i++) {
            String dayName;
            switch (i) {
                case 1:
                    dayName = "MONDAY";
                    break;
                case 2:
                    dayName = "TUESDAY";
                    break;
                case 3:
                    dayName = "WEDNESDAY";
                    break;
                case 4:
                    dayName = "THURSDAY";
                    break;
                case 5:
                    dayName = "FRIDAY";
                    break;
                default:
                    dayName = "NONE";
            }

            int dayNo = i;
            days.add(new Day(dayName, dayNo, lecturePeriods));
        }

        List<YearGroup> yearGroups = new ArrayList<>();
        for (int i = 1; i <= 3; i++) {
            int yearGroupNumber = i;
            if (Objects.equals(i, 1)) {
                yearGroups.add(new YearGroup(yearGroupNumber, new ArrayList<>(Arrays.asList(new CourseGroup("Science" + 1, days)))));
            } else if (Objects.equals(i, 2)) {
                yearGroups.add(new YearGroup(yearGroupNumber, new ArrayList<>(Arrays.asList(new CourseGroup("Science" + 2, days)))));
            } else if (Objects.equals(i, 3)) {
                yearGroups.add(new YearGroup(yearGroupNumber, new ArrayList<>(Arrays.asList(new CourseGroup("Science" + 3, days)))));
            }

        }

        schoolObject = new SchoolObject();
        schoolObject.setYearGroups(yearGroups);
        System.out.println("School Object Before Changing Periods="+schoolObject.toString());

        schoolObject.getYearGroups().forEach(yearGroup -> {
            yearGroup.getCourseGroups().forEach(courseGroup -> {
                courseGroup.getDays().forEach(day -> {
                    String dayName = day.getDayName();
                    if (Objects.equals(dayName, "MONDAY")) {
                        day.getLecturePeriods().forEach(lecturePeriod -> {
                            int lecturePeriodNumber = lecturePeriod.getPeriodNumber();
                            if (Objects.equals(lecturePeriodNumber, 1)) {
                                lecturePeriod.setIsPeriodAllocated(true);

                            }
                        });
                    }
                });
            });
        });
        System.out.println("\nSchool Object After Changing Periods=" + schoolObject.toString());
        final int[] numberOfAllocatedPeriodsExpected = {0};
        schoolObject.getYearGroups().forEach(yearGroup -> {
            yearGroup.getCourseGroups().forEach(courseGroup -> {
                courseGroup.getDays().forEach(day -> {
                    day.getLecturePeriods().forEach(lecturePeriod -> {
                        int lecturePeriodNumber = lecturePeriod.getPeriodNumber();
                        if (Objects.equals(lecturePeriodNumber, 1)) {
                            numberOfAllocatedPeriodsExpected[0]++;
                        }
                    });
                });
            });
        });


        System.out.println("\nTest result ="+Objects.equals(numberOfAllocatedPeriodsExpected[0],3));

    }




    public static class SchoolObject {
        List<YearGroup> yearGroups;

        @Override
        public String toString() {
            final StringBuilder sb = new StringBuilder("SchoolObject{");
            sb.append("yearGroups=").append(yearGroups);
            sb.append('}');
            return sb.toString();
        }

        public List<YearGroup> getYearGroups() {
            return yearGroups;
        }

        public void setYearGroups(List<YearGroup> yearGroups) {
            this.yearGroups = yearGroups;
        }
    }

    public static class YearGroup {

        int yearGroupNumber;
        List<CourseGroup> courseGroups;

        public YearGroup(int yearGroupNumber, List<CourseGroup> courseGroups) {
            this.yearGroupNumber = yearGroupNumber;
            this.courseGroups = courseGroups;
        }

        @Override
        public String toString() {
            final StringBuilder sb = new StringBuilder("YearGroup{");
            sb.append("yearGroupNumber=").append(yearGroupNumber);
            sb.append(", courseGroups=").append(courseGroups);
            sb.append('}');
            return sb.toString();
        }

        public int getYearGroupNumber() {
            return yearGroupNumber;
        }

        public void setYearGroupNumber(int yearGroupNumber) {
            this.yearGroupNumber = yearGroupNumber;
        }

        public List<CourseGroup> getCourseGroups() {
            return courseGroups;
        }

        public void setCourseGroups(List<CourseGroup> courseGroups) {
            this.courseGroups = courseGroups;
        }
    }

    public static class CourseGroup {
        String courseGroupName;
        List<Day> days;

        public CourseGroup(String courseGroupName, List<Day> days) {
            this.courseGroupName = courseGroupName;
            this.days = days;
        }

        @Override
        public String toString() {
            final StringBuilder sb = new StringBuilder("CourseGroup{");
            sb.append("courseGroupName='").append(courseGroupName).append('\'');
            sb.append(", days=").append(days);
            sb.append('}');
            return sb.toString();
        }

        public List<Day> getDays() {
            return days;
        }

        public void setDays(List<Day> days) {
            this.days = days;
        }

        public String getCourseGroupName() {
            return courseGroupName;
        }

        public void setCourseGroupName(String courseGroupName) {
            this.courseGroupName = courseGroupName;
        }
    }

    public static class Day {
        String dayName;
        int dayNo;
        List<LecturePeriod> lecturePeriods;

        public Day(String dayName, int dayNo, List<LecturePeriod> lecturePeriods) {
            this.dayName = dayName;
            this.lecturePeriods = lecturePeriods;
            this.dayNo = dayNo;
        }

        @Override
        public String toString() {
            final StringBuilder sb = new StringBuilder("Day{");
            sb.append("dayName='").append(dayName).append('\'');
            sb.append(", dayNo=").append(dayNo);
            sb.append(", lecturePeriods=").append(lecturePeriods);
            sb.append('}');
            return sb.toString();
        }

        public String getDayName() {
            return dayName;
        }

        public void setDayName(String dayName) {
            this.dayName = dayName;
        }

        public int getDayNo() {
            return dayNo;
        }

        public void setDayNo(int dayNo) {
            this.dayNo = dayNo;
        }

        public List<LecturePeriod> getLecturePeriods() {
            return lecturePeriods;
        }

        public void setLecturePeriods(List<LecturePeriod> lecturePeriods) {
            this.lecturePeriods = lecturePeriods;
        }
    }

    public static class LecturePeriod {
        String periodName;
        int periodNumber;
        boolean isPeriodAllocated;

        public LecturePeriod(String periodName, int periodNumber, boolean isPeriodAllocated) {
            this.periodName = periodName;
            this.periodNumber = periodNumber;
            this.isPeriodAllocated = isPeriodAllocated;
        }

        @Override
        public String toString() {
            final StringBuilder sb = new StringBuilder("LecturePeriod{");
            sb.append("periodName='").append(periodName).append('\'');
            sb.append(", periodNumber=").append(periodNumber);
            sb.append(", isPeriodAllocated=").append(isPeriodAllocated);
            sb.append('}');
            return sb.toString();
        }

        public String getPeriodName() {
            return periodName;
        }

        public void setPeriodName(String periodName) {
            this.periodName = periodName;
        }

        public int getPeriodNumber() {
            return periodNumber;
        }

        public void setPeriodNumber(int periodNumber) {
            this.periodNumber = periodNumber;
        }

        public boolean isPeriodAllocated() {
            return isPeriodAllocated;
        }

        public void setIsPeriodAllocated(boolean isPeriodAllocated) {
            this.isPeriodAllocated = isPeriodAllocated;
        }
    }
}

观察数据的输出,您会注意到,每个courseGroup中的所有日期,而不是仅仅是星期一,schoolObject将所有lecturePeriods设置为true。 任何帮助都感激不尽。 注意:我尽量将问题提取到最小的最小值而不使用gson或jackson,这样可以在梳理生成的json时提供更清晰的输出。 为了测试,我把所有东西都挤成了一个班级。

您可以在此处查看示例http://ideone.com/3uyE5G

2 个答案:

答案 0 :(得分:2)

这是因为您要为所有日子分配相同的讲座时段参考:

days.add(new Day(dayName, dayNo, lecturePeriods));

因此,每当您在其中一天内更改讲话中的一个属性时,它将为所有这些更改。要解决这个问题,每天都必须拥有它自己的讲话实例

List<LecturePeriod> lecturePeriods = new ArrayList<>();
for (int j = 1; j <= 10; j++) {
    lecturePeriods.add(new LecturePeriod("Period " + j, j, false));
}
days.add(new Day(dayName, dayNo, lecturePeriods));

答案 1 :(得分:0)

@ valarauko,你向我展示的是完全正确的,每天都必须拥有它自己的讲话实例,但我找到的一个更具伸缩性的解决方案是,因为该对象可以用不同的数字初始化方式,我做的是

1.将Object转换为json String。

2.在进行任何操作之前将其转换回原始对象。

我使用gson进行上述两个步骤,无论我如何在schoolObject中每天初始化,它都能完美运行。

再次感谢您向我展示@valarauko的根本原因。