-2
我需要得到各行中的最小值的指數在Java中我的二維數組(數組[] [])。我的陣列擁有幾個浮動我是一個初學者,所以請不要恨我並幫助我。謝謝!查找最小值的指數二維數組每一行中在java中
我需要得到各行中的最小值的指數在Java中我的二維數組(數組[] [])。我的陣列擁有幾個浮動我是一個初學者,所以請不要恨我並幫助我。謝謝!查找最小值的指數二維數組每一行中在java中
你可以使用一個輔助方法rowMinsIndex
,你在通過你的二維數組,像這樣:
import java.util.Arrays;
class Main {
public static void main(String[] args) {
int[][] twoDimensionalArray = new int[][]{
{ 1, 2, 5 },
{ 4, 3, 6 },
{ 7, 5, 9 }
};
System.out.println("The twoDimensionalArray is: " + Arrays.deepToString(twoDimensionalArray));
int[] minsOfEachRow = rowMinsIndex(twoDimensionalArray);
for(int i = 0; i < minsOfEachRow.length; i++) {
System.out.println("The index of the minimum of row " + i + " is: " + minsOfEachRow[i]);
}
}
public static int[] rowMinsIndex(int[][] nums) {
int [] count = new int [nums.length];
int [] minIndexes = new int [nums.length];
Arrays.fill(count, Integer.MAX_VALUE);
for(int i = 0; i < count.length; i++){
for(int x = 0; x < nums[0].length; x++){
if(nums[i][x] < count[i]){
count[i] = nums[i][x];
minIndexes[i] = x;
}
}
}
return minIndexes;
}
}
輸出繼電器:
The twoDimensionalArray is: [[1, 2, 5], [4, 3, 6], [7, 5, 9]]
The index of the minimum of row 0 is: 0
The index of the minimum of row 1 is: 1
The index of the minimum of row 2 is: 1
試試吧here!
void findMinimumIndex(float[][] data)
{
int x = 0 , y = 0;
float assumedMin = 0.0f;
int lastSmallestXIndex , lastSmallestYIndex;
ArrayList<Integer> minIndexListInRows = new ArrayList<>();
for(x = 0; x < data.length; x++)
{
for(y = 1 , assumedMin = data[x][y - 1]; y < data[x].length; y++)
{
if(assumedMin < data[x][y])
{
assumedMin = data[x][y];
lastSmallestXIndex = x;
lastSmallestYIndex = y;
}
}
System.out.println("In (" + x + "," + y + "), smallest float is: " + assumedMin + ", At: {" + lastSmallestXIndex + "," + lastSmallestYIndex + "}");
}
}
minIndexListInRows,您可以在其中應用您的技巧以至少發揮你的作用,這個代碼區是你的冒煙槍;
for(y = 1 , assumedMin = data[x][y - 1]; y < data[x].length; y++)
{
if(assumedMin < data[x][y])
{
assumedMin = data[x][y];
lastSmallestXIndex = x;
lastSmallestYIndex = y;
}
}
非常感謝。有用。 –