英文:
How to get the top 5 numbers from a 2D array of random numbers
问题
以下是代码的翻译部分,不包括错误文本:
我对Java还相当陌生,正在学习2D数组。我尝试从一个随机列表中获取前5个数字进行显示。我认为以下代码可能有效,但不确定为什么会出现错误。另一件事是,我不能使用排序函数。
代码如下:
```java
public static void main(String[] args) {
// 随机数部分
Random rand = new Random();
int[] large = new int[5];
int max = 0, index;
int[][] arrSize = new int[4][5];
for (int i = 0; i < arrSize.length; i++) {
for (int j = 0; j < arrSize[i].length; j++) {
arrSize[i][j] = rand.nextInt(89) + 10;
System.out.print(arrSize[i][j] + " ");
}
System.out.println();
}
// 前5个最大数
for (int p = 0; p < 5; p++) {
max = arrSize[0][0];
index = 0;
for (int i = 0; i < arrSize.length; i++) {
for (int j = 0; j < arrSize[i].length; j++) {
if (max < arrSize[i][j]) {
max = arrSize[i][j];
index = i;
}
}
}
large[p] = max;
arrSize[index] = Integer.MIN_VALUE; // 这里出错
System.out.println("最高数字: " + large[p]);
}
}
希望这有助于你理解代码。如果你有其他问题或需要进一步的帮助,请随时提问。
英文:
I am pretty new to java and am just learning 2D arrays. I am trying to get the top 5 numbers to display from a random list. I think this could work but am not sure why I am getting an error. One other thing is that I cannot use the sort function.
Code here:
public static void main(String[] args) {
//Random Number stuff
Random rand = new Random();
int[] large = new int [5];
int max = 0, index;
int[][] arrSize = new int [4][5];
for (int i = 0; i < arrSize.length; i++) {
for (int j=0; j< arrSize[i].length; j++) {
arrSize[i][j] = rand.nextInt(89) + 10;
System.out.print(arrSize[i][j] + " ");
}
System.out.println();
}
// Top 5
for (int p = 0; p < 5; p++) {
max = arrSize [0][0];
index = 0;
for (int i = 0; i < arrSize.length; i++) {
for (int j = 0; j < arrSize[i].length; j++) {
if (max < arrSize[i][j]) {
max = arrSize[i][j];
index = i;
}
}
}
large = max;
arrSize[index] = Integer.MIN_VALUE; //Error here
System.out.println("Highest Number: " + large
);
}
}
}
Error text:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Type mismatch: cannot convert from int to int[]
at secondAssignment.BiggestNumbersRectangular.main(BiggestNumbersRectangular.java:47)
I am not sure why I am getting an error, any help would appreciated. If anyone else has any answers for how I could get the top 5 in a different way that would also be appreciated.
答案1
得分: 1
你在这里声明了你的 arrSize
int[][] arrSize = new int[4][5];
并尝试在这里设置它的值
arrSize[index] = Integer.MIN_VALUE;
arrSize[index]
处的对象是一个数组。
请记住,一个二维数组基本上是这样的:
arrSize
- arrSize[0]
- arrSize[0][0]
- arrSize[0][1]
- arrSize[1]
- arrSize[1][0]
- arrSize[1][1]
- arrSize[2]
- arrSize[2][0]
- arrSize[2][1]
- arrSize[3]
- arrSize[3][0]
- arrSize[3][1]
因为索引是一个单个整数,你实际上是在调用 arrSize[0]
,其中包含 arrSize[0][0]
和 arrSize[0][1]
。
Integer.MIN_VALUE
不是一个整数数组。它是一个 int
。你不能将 int
分配给 int[]
。
英文:
You declare your arrSize
here
int[][] arrSize = new int [4][5];
and try to set it's value here
arrSize[index] = Integer.MIN_VALUE;
The Object at arrSize[index]
is an array.
Remember that a 2D array basically looks like this:
arrSize
- arrSize[0]
- arrSize[0][0]
- arrSize[0][1]
- arrSize[1]
- arrSize[1][0]
- arrSize[1][1]
- arrSize[2]
- arrSize[2][0]
- arrSize[2][1]
- arrSize[3]
- arrSize[3][0]
- arrSize[3][1]
Because index is a single int, you are assentially calling arrSize[0], which contains arrSize[0][0]
and arrSize[0][1]
.
The Integer.MIN_VALUE
is not an array of integers. It is an int
. You cannot assign int
to int[]
.
答案2
得分: 0
如您在代码的其他部分所见,要访问2D数组arrSize中的数值,您需要使用两个索引,分别是i
和j
。
在找到最大数后,您需要保存i
和j
:
if (max < arrSize[i][j]) {
max = arrSize[i][j];
indexI = i;
indexJ = j;
}
接着:
arrSize[indexI][indexJ] = Integer.MIN_VALUE;
关于您遇到错误的原因,arrSize[i]
得到的是一个1D数组。它仍然是一个数组,您不能将一个数组设置为一个整数(Integer.MIN_VALUE
),在Java中,错误消息中表示为int[]
。
算法可以改进,可以使用一个与您想要的最高数字数量相同大小的maxArr
,而不是使用单个整数max
来保存最高值,并使用一个for
循环检查maxArr
中的所有数字,取代原来的:
if (max < arrSize[i][j]) {
max = arrSize[i][j];
index = i;
}
这意味着您可以移除索引(或indexI和indexJ)以及最外层的for
循环(for (int p = 0; p < 5; p++)
)。但这是另一个话题,您应该自己学习这部分。
英文:
As you can see in other parts of the code, to access values in 2D array arrSize, you need 2 indexes in your case (and usually) i
and j
.
You need to save both i
and j
after you find the highest number.
if (max < arrSize[i][j]) {
max = arrSize[i][j];
indexI = i;
indexJ = j;
}
and then
arrSize[indexI][indexJ] = Integer.MIN_VALUE;
As to why you got the error, arrSize[i]
gets you a 1D array. It's still an array and you cannot set an array to an integer (Integer.MIN_VALUE
). in Java represented as int[]
in the error message.
The algorithm could be improved, instead of using a single integer max
for saving the highest value, you could use a maxArr
of the same size as the number of highest numbers you want (in your case 5) and check against all of the numbers in maxArr
using a for in place of
if (max < arrSize[i][j]) {
max = arrSize[i][j];
index = i;
}
That would mean you could remove the index (or indexI and indexJ) and topmost for
cycle (for (int p = 0; p < 5; p++)
). But that's another topic, and one you should learn yourself.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论