为什么从arraylist进行打印时,数组超出边界数组? [英] why do i get an array out of bounds array when printing from arraylist?
问题描述
我似乎无法理解为什么我尝试打印的数组在尝试将其追加到JTextArea时会抛出超出范围的错误.
i cant seem to understand why the array i am trying to print throws an array out of bounds error when i try to append it to a JTextArea.
两个数组都已初始化为从零开始.
Both arrays have been initialized to start at zero.
private ArrayList<String> UniResponse = new ArrayList(0);
private ArrayList<String> CHOICES = new ArrayList(0);
uniPicksString,UniResponse和CHOICES是从其他对象传递来的数组,但是它们也已初始化为从0开始;
uniPicksString, UniResponse, and CHOICES are arrays that have been passed from a different object, however they have also been initialized to start at 0;
此方法以字符串类型返回数组列表的元素
this method returns the elements of the array list as a string type
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的字符串的方法
this is the method that creates the string that is appended to 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?
is it possible to create a try catch statement that will ignore the error thrown and print the arraylist?
任何建议都非常感谢.
推荐答案
正如评论所说,applicantArray
最有可能小于choices
.
As the comments say, most likely the applicantArray
is smaller then choices
.
仍然,count
等于UniResponse.size()
,因此while
循环条件从一开始就评估为false
,因此您返回了null
值.
Still, count
equals UniResponse.size()
therefore the while
loop condition evaluates to false
from the beginning and so you return a null
value.
不,它不会改变任何东西,很难理解您的需求...
No that it changes anything, it's hard to understand what you need...
是否可以创建一条try catch语句,该语句将忽略抛出的错误并打印arraylist?
is it possible to create a try catch statement that will ignore the error thrown and print the arraylist?
try / catch
与不相处忽略忽略错误
此外,第二个函数中的if
语句也没有用.在if
和else
主体中,您所做的事情都非常相同.
Also, the if
statement in your second function is useless. You do the very same in thing in both the if
and the else
body.
这篇关于为什么从arraylist进行打印时,数组超出边界数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!