Java 二维数组索引越界异常问题
我正在制定一个计划来制定时间表;我通过将员工添加到列表中来接受用户输入,但是当我尝试将项目从列表移动到双数组时,我得到了一个数组索引越界异常。我能在这里做些什么来解决这个问题,我已经尝试了我能想到的一切Java 二维数组索引越界异常问题,java,arrays,Java,Arrays,我正在制定一个计划来制定时间表;我通过将员工添加到列表中来接受用户输入,但是当我尝试将项目从列表移动到双数组时,我得到了一个数组索引越界异常。我能在这里做些什么来解决这个问题,我已经尝试了我能想到的一切 import java.util.*; public class schedule { public static void main(String[] args) { Scanner readme = new Scanner(System.in);
import java.util.*;
public class schedule
{
public static void main(String[] args)
{
Scanner readme = new Scanner(System.in);
List<String> employees = new ArrayList<String>();
//accepts input for list
while(true)
{
System.out.println("Employees are: " + employees);
System.out.println("Are there more? (y/n)");
if (readme.next().startsWith("y"))
{
System.out.println("Who?");
employees.add(readme.next());
}
else
{
//creates a double array the size of the employees list by the number of shifts in the week
String[][] scheduleArr = new String[employees.size()][14];
//adds items from array to corresponding spot on double array
for (int i = 0; i <= scheduleArr.length; i++)
{
scheduleArr[i][0] = employees.get(i);
}
System.out.print(scheduleArr[0][0]);
}
}
}
}
import java.util.*;
公课时间表
{
公共静态void main(字符串[]args)
{
扫描仪自述文件=新扫描仪(System.in);
List employees=new ArrayList();
//接受列表的输入
while(true)
{
System.out.println(“员工为:“+员工”);
System.out.println(“还有吗?(y/n)”);
if(readme.next().startsWith(“y”))
{
System.out.println(“谁?”);
add(readme.next());
}
其他的
{
//创建一个双数组,该数组的大小为员工列表的大小(按一周的班次)
字符串[][]scheduleArr=新字符串[employees.size()][14];
//将数组中的项目添加到双数组上的相应点
对于(int i=0;i数组索引从0开始,在长度-1结束
改变
for (int i = 0; i <= scheduleArr.length; i++)
for(int i=0;i您应该从0迭代到length-1
而不是length
(数组基于0)
for(int i=0;i
for (int i = 0; i < scheduleArr.length; i++) //NOT i <= scheduleArr.length
{
scheduleArr[i][0] = employees.get(i);
}
for(int i=0;i for (int i = 0; i < scheduleArr.length; i++) //NOT i <= scheduleArr.length
{
scheduleArr[i][0] = employees.get(i);
}
for (int i = 0; i < scheduleArr.length; i++) //NOT i <= scheduleArr.length
{
scheduleArr[i][0] = employees.get(i);
}
for (int i = 1; i <= scheduleArr.length; i++) //NOT i <= scheduleArr.length
{
scheduleArr[i][0] = employees.get(i);
}