2015-11-06 85 views
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

+0

你为什么不使用的for-each: '的for(int i均有:一个){...' –

回答

5
for(i=0; 1< a.size(); i++) 

应该for(i=0; i< a.size(); i++)

+0

哇。好吧,我不相信我没有看到!十分感谢你的帮助!我以此为标志,我应该睡一会儿哈哈 – Nick