gpt4 book ai didi

java - 用java将值输入到二维数组中

转载 作者:行者123 更新时间:2023-12-01 18:35:41 26 4
gpt4 key购买 nike

我有一个带有 createSchedule 方法的类(class)表,该方法应该创建整周的时间表。我正在使用嵌套的 for 循环来完成这项工作,但遇到了这种类型的错误

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at com.sib.TManager.Schedule.createSchedule(Schedule.java:58)
at com.sib.TManager.App.main(App.java:32)

这是我的代码,我不知道它出了什么问题。有人可以帮忙吗?

  public class Schedule{    
private String[][] scheduleList;
private final String[] DAYS={"Monday","Tuesday","Wednesday","Thursday","Friday"};
public void createSchedule(){
Random rdm= new Random();
ArrayList<String> workstationlist = new ArrayList<String>();
ArrayList<String> employeelist = new ArrayList<String>();
Scanner s;
//load employees into a list
try {
s = new Scanner(new File("EmployeeList.txt"));

while (s.hasNext()){
employeelist.add(s.next());
}
s.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//load workstations into a list
try {
s = new Scanner(new File("WorkStationList.txt"));

while (s.hasNext()){
workstationlist.add(s.next());
}
s.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//
System.out.println("List of employees");
for (Iterator iterator = employeelist.iterator(); iterator.hasNext();) {
System.out.println(iterator.next().toString());

}
System.out.println();
System.out.println("List of workstations");
for (Iterator iterator = workstationlist.iterator(); iterator.hasNext();) {
System.out.println(iterator.next().toString());
}
String[][] mySchedule = new String[DAYS.length][employeelist.size()];
//creation of the schedule for the week
for (int i = 0; i < DAYS.length; i++) {
for (int j=0; j< employeelist.size(); i++)
{
mySchedule[i][j]="Test";
}
}
//printing schedule in console
for (int i = 0; i < DAYS.length; i++) {
for (int j=0; j< employeelist.size(); j++)
{
System.out.println(mySchedule[i][j]);
}
}
}
}
public String[][] getScheduleList() {
return scheduleList;
}
public void setScheduleList(String[][] scheduleList) {
this.scheduleList = scheduleList;
}


}

最佳答案

for (int j=0; j< employeelist.size(); i++) 
{
mySchedule[i][j]="Test";
}

你应该在这里增加i吗?

关于java - 用java将值输入到二维数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22138786/

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