![](/img/trans.png)
[英]Java find min and max value in 2d array. Minimum value found is not correct
[英]Java: Counting (and “removing”) max-/minimum value of 2D array
我希望該方法對2D數組poängInsamling
中的所有最大值/最小值進行計數。 每個domare
(=法官)為所有deltagare
(= members)賦予一個值。 我想刪除每個deltagare
的maximum/minimum
。 我現在擁有的代碼僅在有2
或更少成員的情況下有效。
這就是我得到的:
for (int x = 0; x < deltagare; x++) {
for (int y = 0; y < domare; y++) {
if (poängInsamling[y][x] == -1) {
poängInsamling[y][x] = 0;
break;
}
}
}
return poängInsamling;
}
在此先感謝您,我已經嘗試修復了幾個小時。
編輯: int[][]PoängInsamling = int[domare][deltagare];
如果所有deltagare具有相同的值,則它們的所有點均以0結尾。
您正在搜索整個2D數組,以便刪除所有成員的最低和最高價值,但是您只想刪除當前成員的最低和最高價值。 如果您跟蹤具有最大值/最小值的索引,則可以消除第二個循環。
例如,對於最大值(最小值將類似):
int max = -1;
int maxIndex = -1;
for(int i = 0; i < deltagare; i++) {
max = -1; // clear the max value when starting with a new member
maxIndex = -1;
for(int t = 0; t < domare; t++) {
if (poängInsamling[t][i] > max) {
max = poängInsamling[t][i];
maxIndex = t;
}
}
// clear the max value for the current member
poängInsamling[maxIndex][i] = -1;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.