为什么我从arraylist打印时得到一个数组超出边界数组?

时间:2018-03-08 08:18:02

标签: java swing arraylist error-handling indexoutofboundsexception

我似乎无法理解为什么我尝试打印的数组在我尝试将其附加到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?

非常感谢任何建议。

3 个答案:

答案 0 :(得分:2)

正如评论所说,applicantArray很可能小于choices

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

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

  

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

try / catch不能与忽略错误

相处

此外,第二个函数中的if语句也没用。你在ifelse身体中做同样的事情。

答案 1 :(得分:0)

我不会认为以下内容会被执行。在for循环结束之前,计数值等于UniResponse.size()。所以情况会失败。

  

while(itr.hasNext()&amp;&amp; count

我认为你在方法returnProfile()上得到索引超出界限。 您正在迭代CHOICES,并在内部使用另一个变量ApplicantArray。 CHOICES和ApplicantArray的大小相同吗?如果没有,且ApplicantArray大小小于CHOICES,那么你将陷入界外异常。

答案 2 :(得分:0)

J J怎么说我认为这个条件:

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

使&#34;持有&#34;保持空虚,因为你使用&#34; count&#34;之前。 &#34; for&#34;句子完成因为&#34;计数&#34;等于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而不是+或+ =

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

:)