0
我正在转换一个写入数组的方法,用于查找从main传递的ArrayList的模式。一路上我正在击中和索引错误。我确定它是一个明显的错误,但只是没有看到它。会爱另一双眼睛在这!无法看到IndexOutOfBoundsException
public static int getMode(ArrayList<Integer> a)
{
int i ,j ,ctr=0 ,wantedScore ,maxsofar ,position=0 ,mode;
ArrayList<Integer> ctrArray = new ArrayList<>(a.size());
for(i=0; 1< a.size(); i++)
{
wantedScore = a.get(i);
for(j=i+1; j < a.size(); j++)
{
if(a.get(i)==wantedScore)
{
ctr++;
}//End IF
ctrArray.add(ctr);
}//End Inner Loop
}//End Outer For Loop
//Find highest value counter
maxsofar=a.get(0);
for(i=0; i< a.size(); i++)
{
if(ctrArray.get(i)>maxsofar)
{
maxsofar=ctrArray.get(i);
position=i;
}//End If
}//End For Loop
if(maxsofar>0)
mode=a.get(position);
else
mode=-1;
return mode;
}//End getMode
我越来越如果数组列表中包含值1,2,3是错误: 异常线程 “main” java.lang.IndexOutOfBoundsException:指数:3,大小:3
你为什么不使用的for-each: '的for(int i均有:一个){...' –