我的目标是打印一个包含24小时十进制格式的进入和退出时间的列表列表,从" AM" - " PM"用户输入的字符串格式为String数组,如下所示: {上午6点至上午8点,上午11点至下午1点,上午7点至下午8点,上午7点至上午8点,上午10点至下午12点,下午12点至下午4点,下午1点至下午4点,上午8点至上午9点}
我在for循环中声明了各个列表,并在循环中为它们赋值,但是从我的代码中获得了以下运行时异常: java.lang.IndexOutOfBoundsException:Index:0,Size:0
请帮我调试我的代码:
import java.util.*;
public class TimeSchedule
{
public static List<List<Integer>> Timein24hourFormat(String[] input1)
{
List<List<Integer>> scheduledTime = new ArrayList<List<Integer>>();
int [] exitTime = new int[input1.length];
int [] entryTime = new int[input1.length];
for (int i=0;i<input1.length;i++)
{
List<String> listOfStrings = new ArrayList<>();
List<Integer> tempList = scheduledTime.get(i);
String[] timeSlot = input1[i].split("#");
for (int m=0;m<2;m++)
{
listOfStrings.add(timeSlot[m]);
if (listOfStrings.contains("AM"))
{
listOfStrings.remove("AM");
tempList.add(Integer.parseInt(listOfStrings.get(m)));
}
if (listOfStrings.contains("PM") && timeSlot[m].contains("12"))
{
listOfStrings.remove("PM");
tempList.add(Integer.parseInt(listOfStrings.get(m)));
}
if (listOfStrings.contains("PM") && !timeSlot[m].contains("12"))
{
listOfStrings.remove("PM");
tempList.add((Integer.parseInt(listOfStrings.get(m))) + 12);
}
}
}
return scheduledTime;
}
public static void main (String[]args)
{
Scanner input = new Scanner(System.in);
int customersNumber = input.nextInt();
input.nextLine();
String [] entryExitTime = new String[customersNumber];
for (int i=0;i<customersNumber;i++)
{
entryExitTime[i] = input.nextLine();
}
System.out.println(Timein24hourFormat(entryExitTime));
}
}
答案 0 :(得分:0)
public static List<List<Integer>> Timein24hourFormat(String[] input1)
{
List<List<Integer>> scheduledTime = new ArrayList<List<Integer>>();
int [] exitTime = new int[input1.length];
int [] entryTime = new int[input1.length];
for (int i=0;i<input1.length;i++)
{
List<String> listOfStrings = new ArrayList<>();
List<Integer> tempList = scheduledTime.get(i);
String[] timeSlot = input1[i].split("#");
scheduledTime 在此阶段为空,这就是您无法检索值并获得 IndexOutOfBoundsException
的原因