これが私がそれを行う方法についてのいくつかのアイデアです。
データを整数の2次元配列に保存するとしましょう
int [] [] data = new int [rows] [columns]
したがって、この場合、列は A、B、C などです。
最大値を検索するときは、次のように繰り返す必要があります。
data[i][fix]
そのため、列は固定され、行はループで変更されます
あなたの例では、最大値を取得したい場合、A
私がアドバイスしたように2次元配列を使用すると、次のようになります。
int [] [] data = new int [3][3];
A
次に、 fromの最大値を取得する必要があるセットはdata [0][0]
でdata[1][0]
あり、data[2][0]
編集:
ここに考えられる解決策の 1 つがあります。
//here is our data array.
int [][] data = new int[3][];
//fill up with som random data
data[0] = new int[]{10,20,4,5,56,87,9};
data[1] = new int[]{1,65,0,10,3};
data[2] = new int[]{34,5,6,67,3,54};
//get the biggest arrays length
int maxArrayLength = 0;
for(int i=1; i<data.length; i++)
{
if(data[i].length > data[maxArrayLength].length)
maxArrayLength = i;
}
maxArrayLength = data[maxArrayLength].length;
//collect the max in each category
int [] categoryMax = new int[maxArrayLength];
//loop through the categories
for(int i=0; i<maxArrayLength; i++)
{
//in each iteration we get a winner
int categoryWinner = 0;
// now loop through the values of the current category
for(int j=0; j<data.length; j++)
{
int [] actualArray = data[j];
int [] winnerArray = data[categoryWinner];
//check if we are in the bounds, if so, then perform a simple maxsearch
if(i<actualArray.length)
if(actualArray[i] > winnerArray[i])
categoryWinner = j;
}
//set the current element of the winners array.
categoryMax [i] = data[categoryWinner][i];
}
int sum = 0;
// we are ready, print it out!
for(int i=0; i<categoryMax.length; i++)
{
System.out.println((i+1) + ". groups winner: " + categoryMax[i]);
sum+=categoryMax[i];
}
System.out.println(sum);