私の Java コードでは、データが与えられ、モードを見つける必要があります。すべてが正常にコンパイルされ、すべてのメソッドが機能します。ただし、モードにアクセスしようとするとjava.lang.ArrayIndexOutOfBoundsException: 987
、ターミナル ウィンドウに が表示されます。強調表示されている部分は、私の最大の方法の 1 つである次の方法にあります。ちなみに、データ配列は単なるint []
データです。
public int maxOfData(int [] oa)
{
int max = oa[0];
for (int i = 1; i < size; i++)
{
if (oa[i] > max)
max = oa[i];
}
return max;
}
例外は行にありますif(oa[i] > max)
モードコードは次のとおりです。
public int[] modeOfData()
{
int[] tally = new int[maxOfData() + 1];
for( int i = 0; i < size; i++)
{
tally[data[i]]++;
}
//max contains frequency of modes
int max = maxOfData (tally);
int count = 0;
for( int i = 0; i < tally.length; i++)
{
if( tally[i] == max )
count++;
}
//count occurence of maxValue in tally (for)
//that determines how many modes there are
//declare another int called modes
int[] modes = new int[count];
//size of array should be count
//loop through tally and extract modes: it's the index value.
int pos = 0;
for( int i = 0; i < tally.length; i++)
{
if(tally[i] == count)
modes[pos++] = i;
}
return modes;
//modes are where the values are == max
}
私の他の最大値data
は同じですが、data
代わりにoa
. 私の先生によると、私は両方の最大メソッドが必要です。それで、私は何をしますか?これを修正するにはどうすればよいですか?