简体   繁体   English

使用Java将值输入二维数组

[英]input values into a two dimensional array with java

I have a class schedule with a method createSchedule that is supposed to create a schedule for the whole week. 我有一个带有方法createSchedule的课程表,该方法应该为整个星期创建一个时间表。 I am using a nested for loop to do the job but I am having an error of this type 我正在使用嵌套的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)

here is my code and I can't figure out what is wrong with it. 这是我的代码,我无法弄清楚它出了什么问题。 Can someone help please? 有人可以帮忙吗?

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

Are you supposed to increment i here? 你应该在这里增加i吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM