gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:08:23 25 4
gpt4 key购买 nike

我试图将每个 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;
}
}
}

观察数据的输出,您会注意到 schoolObject 将每个 courseGroup 中所有日期的所有 LecturePeriods 设置为 true,而不仅仅是星期一。任何帮助都感激不尽。注意:我尽力将问题提取到最低限度,而不使用 gson 或 jackson,这将在梳理生成的 json 时提供更清晰的输出。为了测试起见,我将所有内容压缩到一个类中。

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

最佳答案

这是因为您为所有日期分配相同的讲座时间引用:

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

因此,每次您在某一天更改 LecturePeriods 对象中的属性时,所有这些属性都会发生更改。为了解决这个问题,每天都必须有自己的 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));

关于java - 我正在更改对象的一部分,但它不断更改列表中的所有对象,而不考虑条件集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41420895/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com