【问题标题】:why do i get an array out of bounds array when printing from arraylist?为什么从arraylist打印时数组超出范围?
【发布时间】:2018-08-16 12:42:30
【问题描述】:

我似乎无法理解为什么我尝试打印的数组在我尝试将其附加到 JTextArea 时会引发数组越界错误。

两个数组都已初始化为从零开始。

          private ArrayList<String> UniResponse = new ArrayList(0);
          private ArrayList<String> CHOICES = new ArrayList(0);

uniPicksString、UniResponse 和 CHOICES 是从不同对象传递过来的数组,但它们也被初始化为从 0 开始;

此方法将数组列表的元素作为字符串类型返回

String receiveAdminResponse() {
    Iterator<String> itr = UniResponse.iterator();
    String hold = null;
    int count=0;
    System.out.println("\n  inside Student class = pringint decision array");

    for (count = 0;count < UniResponse.size(); count++) {
        System.out.println(UniResponse.get(count));
    }

    while(itr.hasNext() && count<UniResponse.size()) {
        hold = UniResponse.get(count);  
    }
    return hold;

}

这是创建附加到 JTextArea 的字符串的方法

String returnProfile() {
    String studentInfo =FAMILYNAME+", " + "average = " + AVERAGE + " \n";
    String uniPicksString ="";

    for (int i = 0; i<CHOICES.size(); i++) {            
        if(i<CHOICES.size() - 1) {
            uniPicksString = uniPicksString +CHOICES.get(i)+ ": " + ApplicantArray.get(i).receiveAdminResponse();//"admin decision, " ;
        }else {
            uniPicksString = uniPicksString + CHOICES.get(i)+ ": " +  ApplicantArray.get(i).receiveAdminResponse();//"admin decision" + "\n" ;
        }   
    }   
    return studentInfo  + uniPicksString + "\n";
}

是否可以创建一个try catch 语句来忽略抛出的错误并打印arraylist?

非常感谢任何建议。

【问题讨论】:

  • 除此之外,现在是开始遵循 Java 命名约定的好时机。尽早养成良好的习惯很重要。接下来,您能否提供minimal reproducible example 而不仅仅是 sn-ps,并确保您显示确切的异常,包括堆栈跟踪?您在显示的代码中调用了 6 次 ArrayList.get(),但我们看不到哪个抛出了异常。
  • 很可能您的 ApplicantArrayCHOICES 数组小

标签: java swing arraylist error-handling indexoutofboundsexception


【解决方案1】:

正如 cmets 所说,applicantArray 很可能比 choices 小。

仍然,count 等于 UniResponse.size(),因此 while 循环条件从一开始就计算为 false,因此您返回一个 null 值。

不,它改变了任何东西,很难理解你需要什么......

是否可以创建一个try catch语句来忽略抛出的错误并打印arraylist?

try / catch 不和忽略错误


另外,第二个函数中的 if 语句是无用的。你在 ifelse 正文中做同样的事情。

【讨论】:

    【解决方案2】:

    我认为下面的 while 不会被执行。在 for 循环结束之前,计数值等于 UniResponse.size()。所以条件会失败。

    while(itr.hasNext() && 计数

    我认为您在 returnProfile() 方法上的索引超出了范围。 您正在通过 CHOICES 进行迭代,并且在其中您正在使用另一个变量 ApplyArray。 CHOICES 和 ApplyArray 的大小是否相同?如果不是并且申请人数组的大小小于 CHOICES,那么您将陷入越界异常。

    【讨论】:

    • ApplicantArray 是一个对象数组列表,每个对象都包含一个数组 CHOICES 这些对象所做的。 ArrayList.get(i).returnProfile() 旨在单独返回对象所做的选择。
    【解决方案3】:

    J J 怎么说我认为这种情况:

    while (itr.hasNext() && count<UniResponse.size())
    

    使“保持”保持空白,因为您之前使用过“计数”。 “for”语句结束,因为“count”等于 UniResponse.size()。另外认为你不需要使用itr.hasNexT(),所以你可以在for循环中做所有事情。我会这样写:

    String receiveAdminResponse() {
      String hold = null;
    
      if (UniResponse.size() > 0) {
        System.out.println("\n  inside Student class = pringint decision array");
    
        for (int count = 0;count < UniResponse.size(); count++) {
          System.out.println(UniResponse.get(count));
          /* If you use '=' you are setting the variable to the last value 
          of UniResponse.get(count) */
          hold += UniResponse.get(count) + ", "; // ',' or '\n" the separator you want  
        }
      }
    
      return hold;
    }
    

    试试这个,对于 returnProfile 方法:

    String returnProfile() {
      String studentInfo =FAMILYNAME+", " + "average = " + AVERAGE + " \n";
      String uniPicksString ="";
    
      if (ApplicantArray.size() < CHOICES.size()) {
        System.out.println("ApplicantArray size is lower than CHOICES (" + ApplicantArray.size() + ", " + CHOICES.size + ").");
      }
      else if (ApplicantArray.size() == CHOICES.size()) {
        for (int i = 0; i < CHOICES.size(); i++) {            
          if(i < CHOICES.size() - 1) {
            uniPicksString += uniPicksString + CHOICES.get(i) + ": " + ApplicantArray.get(i).receiveAdminResponse() + ", " ;
          }else {
            uniPicksString += uniPicksString + CHOICES.get(i) + ": " +  ApplicantArray.get(i).receiveAdminResponse() + "\n" ;
          }   
        }   
      }
      else System.out.println("ApplicantArray size is bigger than CHOICES (" + ApplicantArray.size() + ", " + CHOICES.size + ").");
    
      return studentInfo  + uniPicksString + "\n";
    }
    

    为了获得更好的性能,请使用 StringBuilder 代替 + 或 +=

    祝你好运,对不起我的英语水平!

    :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-19
      • 1970-01-01
      • 1970-01-01
      • 2022-08-10
      • 1970-01-01
      相关资源
      最近更新 更多