【问题标题】:How to add items to a list of lists如何将项目添加到列表列表
【发布时间】:2017-07-14 08:15:47
【问题描述】:

我的目标是从用户输入的“AM”-“PM”字符串格式以字符串数组的形式打印包含 24 小时十进制格式的进入和退出时间的列表列表,如下所示: {6AM#8AM、11AM#1PM、7AM#8PM、7AM#8AM、10AM#12PM、12PM#4PM、1PM#4PM、8AM#9AM}

我在 for 循环中声明了各个列表,并在循环中为它们分配了值,但从我的代码中得到了以下运行时异常: java.lang.IndexOutOfBoundsException:索引:0,大小: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));
  }
 }

【问题讨论】:

  • 创建List&lt;List&lt;Integer&gt;&gt; 不会实例化任何内部List&lt;Integer&gt;s。您需要将new List&lt;Integer&gt; 添加到基于input1.length 的外部列表中。
  • for (int j=0;j&lt;input1.length;j++) { scheduledTime.add(new ArrayList&lt;Integer&gt;()); }
  • 我尝试在scheduledTime 声明之后添加上述行。这会继续抛出异常。

标签: java list arraylist collections


【解决方案1】:
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

【讨论】:

  • for (int j=0;j&lt;input1.length;j++) { scheduledTime.add(new ArrayList&lt;Integer&gt;()); }
  • 我在声明scheduledTime后添加了上面的行。现在如何给 tempList 赋值?
  • 你能告诉我你的预期结果是什么吗?实际上代码是不可读的并且写得不好。
  • 抱歉我的代码格式不正确。我摆脱了异常,但在从listOfStrings 中删除“AM”和“PM”时遇到了麻烦。你能为此建议合适的内置方法吗?
  • 代码中的条件永远不会满足,因为您的列表包含“7AM”之类的值而不是“AM”。所以下一行和下一行不适合这种情况。 if (listOfStrings.contains("AM"))
猜你喜欢
  • 2018-04-04
  • 1970-01-01
  • 2017-08-26
  • 2019-11-26
  • 2017-02-20
  • 1970-01-01
相关资源
最近更新 更多