从一个随机数字的二维数组中获取前5个数字。

huangapple go评论93阅读模式
英文:

How to get the top 5 numbers from a 2D array of random numbers

问题

以下是代码的翻译部分,不包括错误文本:

  1. 我对Java还相当陌生正在学习2D数组我尝试从一个随机列表中获取前5个数字进行显示我认为以下代码可能有效但不确定为什么会出现错误另一件事是我不能使用排序函数
  2. 代码如下
  3. ```java
  4. public static void main(String[] args) {
  5. // 随机数部分
  6. Random rand = new Random();
  7. int[] large = new int[5];
  8. int max = 0, index;
  9. int[][] arrSize = new int[4][5];
  10. for (int i = 0; i < arrSize.length; i++) {
  11. for (int j = 0; j < arrSize[i].length; j++) {
  12. arrSize[i][j] = rand.nextInt(89) + 10;
  13. System.out.print(arrSize[i][j] + " ");
  14. }
  15. System.out.println();
  16. }
  17. // 前5个最大数
  18. for (int p = 0; p < 5; p++) {
  19. max = arrSize[0][0];
  20. index = 0;
  21. for (int i = 0; i < arrSize.length; i++) {
  22. for (int j = 0; j < arrSize[i].length; j++) {
  23. if (max < arrSize[i][j]) {
  24. max = arrSize[i][j];
  25. index = i;
  26. }
  27. }
  28. }
  29. large[p] = max;
  30. arrSize[index] = Integer.MIN_VALUE; // 这里出错
  31. System.out.println("最高数字: " + large[p]);
  32. }
  33. }

希望这有助于你理解代码。如果你有其他问题或需要进一步的帮助,请随时提问。

英文:

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:

  1. public static void main(String[] args) {
  2. //Random Number stuff
  3. Random rand = new Random();
  4. int[] large = new int [5];
  5. int max = 0, index;
  6. int[][] arrSize = new int [4][5];
  7. for (int i = 0; i &lt; arrSize.length; i++) {
  8. for (int j=0; j&lt; arrSize[i].length; j++) {
  9. arrSize[i][j] = rand.nextInt(89) + 10;
  10. System.out.print(arrSize[i][j] + &quot; &quot;);
  11. }
  12. System.out.println();
  13. }
  14. // Top 5
  15. for (int p = 0; p &lt; 5; p++) {
  16. max = arrSize [0][0];
  17. index = 0;
  18. for (int i = 0; i &lt; arrSize.length; i++) {
  19. for (int j = 0; j &lt; arrSize[i].length; j++) {
  20. if (max &lt; arrSize[i][j]) {
  21. max = arrSize[i][j];
  22. index = i;
  23. }
  24. }
  25. }
  26. large

    = max;

  27. arrSize[index] = Integer.MIN_VALUE; //Error here

  28. System.out.println(&quot;Highest Number: &quot; + large

    );

  29. }

  30. }

  31. }

Error text:

  1. Exception in thread &quot;main&quot; java.lang.Error: Unresolved compilation problem:
  2. Type mismatch: cannot convert from int to int[]
  3. 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

  1. int[][] arrSize = new int[4][5];

并尝试在这里设置它的值

  1. arrSize[index] = Integer.MIN_VALUE;

arrSize[index] 处的对象是一个数组。

请记住,一个二维数组基本上是这样的:

  1. arrSize
  2. - arrSize[0]
  3. - arrSize[0][0]
  4. - arrSize[0][1]
  5. - arrSize[1]
  6. - arrSize[1][0]
  7. - arrSize[1][1]
  8. - arrSize[2]
  9. - arrSize[2][0]
  10. - arrSize[2][1]
  11. - arrSize[3]
  12. - arrSize[3][0]
  13. - arrSize[3][1]

因为索引是一个单个整数,你实际上是在调用 arrSize[0],其中包含 arrSize[0][0]arrSize[0][1]

Integer.MIN_VALUE 不是一个整数数组。它是一个 int。你不能将 int 分配给 int[]

英文:

You declare your arrSize here

  1. int[][] arrSize = new int [4][5];

and try to set it's value here

  1. arrSize[index] = Integer.MIN_VALUE;

The Object at arrSize[index] is an array.

Remember that a 2D array basically looks like this:

  1. arrSize
  2. - arrSize[0]
  3. - arrSize[0][0]
  4. - arrSize[0][1]
  5. - arrSize[1]
  6. - arrSize[1][0]
  7. - arrSize[1][1]
  8. - arrSize[2]
  9. - arrSize[2][0]
  10. - arrSize[2][1]
  11. - arrSize[3]
  12. - arrSize[3][0]
  13. - 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中的数值,您需要使用两个索引,分别是ij

在找到最大数后,您需要保存ij

  1. if (max < arrSize[i][j]) {
  2. max = arrSize[i][j];
  3. indexI = i;
  4. indexJ = j;
  5. }

接着:

  1. arrSize[indexI][indexJ] = Integer.MIN_VALUE;

关于您遇到错误的原因,arrSize[i]得到的是一个1D数组。它仍然是一个数组,您不能将一个数组设置为一个整数(Integer.MIN_VALUE),在Java中,错误消息中表示为int[]

算法可以改进,可以使用一个与您想要的最高数字数量相同大小的maxArr,而不是使用单个整数max来保存最高值,并使用一个for循环检查maxArr中的所有数字,取代原来的:

  1. if (max < arrSize[i][j]) {
  2. max = arrSize[i][j];
  3. index = i;
  4. }

这意味着您可以移除索引(或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.

  1. if (max &lt; arrSize[i][j]) {
  2. max = arrSize[i][j];
  3. indexI = i;
  4. indexJ = j;
  5. }

and then

  1. 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

  1. if (max &lt; arrSize[i][j]) {
  2. max = arrSize[i][j];
  3. index = i;
  4. }

That would mean you could remove the index (or indexI and indexJ) and topmost for cycle (for (int p = 0; p &lt; 5; p++)). But that's another topic, and one you should learn yourself.

huangapple
  • 本文由 发表于 2023年2月16日 04:05:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75464938.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定