简体   繁体   English

初学者Java-遍历数组

[英]Beginner Java - looping through an array

So this class is supposed to print out the largest number in the array defined under the main method (aka 22) however when I run it nothing happens. 因此,该类应该在main方法(也称为22)下定义的数组中打印出最大的数字,但是当我运行它时,什么也没有发生。 I'm sure this is a very stupid question but today is my first day with java and I have already spent an embarrassing amount of time trying to figure it out. 我敢肯定这是一个非常愚蠢的问题,但是今天是我使用Java的第一天,我已经花了很多令人费解的时间来解决它。 Thanks! 谢谢!

  public class fun {
        public static void main (String [] args) {
        int[] numbers = new int[] {9, 2, 15, 2, 22, 10, 6};
        max(numbers);
        }

        public static int max(int[] m) {
            int length = m.length;
            int counter = 1;
            int currMax = m[0];
            while (counter <= (length - 2)){
                if (m[counter] > currMax){
                currMax = m[counter];
                }
                counter = counter + 1;
            }
            return currMax;
        }
    }

Your max method does indeed return the max number, but you're not doing anything with that number. 您的max方法确实返回了最大数,但是您没有对该数字做任何事情。

Perhaps you wish to print out the max? 也许您希望打印出最大值?

System.out.println(max(numbers));

In your main method get the return value from max method in one variable then print it 在您的main方法中,从max方法中获得一个变量的返回值,然后打印出来

 public static void main (String [] args) {
    int[] numbers = new int[] {9, 2, 15, 2, 22, 10, 6};
   int val = max(numbers);
   System.out.println(val);
    }

Bingo... 答对了...

You have to print out the number as in 您必须按照以下步骤打印号码

System.out.println(max(numbers));

Alternatively you can assign it to an int variable and then do stuff with it: 另外,您可以将其分配给一个int变量,然后对其进行处理:

int maxNumber = max(numbers);
  public class fun {
        public static void main (String [] args) {
        int[] numbers = new int[] {9, 2, 15, 2, 22, 10, 6};
        int maxNum = max(numbers); //change this
System.out.println(maxNum);
        }

        public static int max(int[] m) {
            int length = m.length;
            int counter = 1;
            int currMax = m[0];
            while (counter <= (length - 1)){ //change this
                if (m[counter] > currMax){
                currMax = m[counter];
                }
                counter = counter + 1;
            }
            return currMax;
        }
    }

The iterration itself could work properly if you would iterrate through the entire Array. 如果您要遍历整个数组,则迭代本身可以正常工作。 You are missing the last index of your Array. 您缺少数组的最后一个索引。 So it won't compare the Array[on_last_index] with the currentMax value anymore. 因此,它将不再将Array [on_last_index]与currentMax值进行比较。

To solve this simple problem all you need to do is to change the condition in the while loop. 要解决这个简单的问题,您所需要做的就是更改while循环中的条件。

while(counter <= length-1) { ... }

So let me try to explain it to you. 因此,让我尝试向您解释。 If you create an Array of follow elements inside it: 如果在其中创建跟随元素数组:

numbers = new int[]{9, 2, 15, 2, 22, 10, 6};

then every single value in your array got it's own index. 那么数组中的每个值都有其自己的索引。 For instance if you want to get the first element of your array, then all you have to do is get the element of the array on index 0. 0 because it is the first element that you want to pick => index 0 is the first index for the first element in your array. 例如,如果您要获取数组的第一个元素,那么您要做的就是获取索引为0的数组的元素。0是因为它是您要选择的第一个元素=>索引0是第一个数组中第一个元素的索引。 Now at your example you got an array of the size 7 => you got 7 elements inside your array. 现在在您的示例中,您得到了一个大小为7 =>的数组,您在该数组中有7个元素。 But remember how to get elements of your array? 但是还记得如何获取数组的元素吗? Right! 对! You can get elements by their indizes. 您可以通过它们的元素获取元素。 The first element got the index 0, the secound 1,...,the last 6. 6 becuase we started at 0, so if you want to iterrate threw an array then always from 0 to array.length-1. 第一个元素的索引为0,第二个元素的索引为...,。,最后一个6。6,因为我们从0开始,所以如果要迭代,则将一个数组抛出,然后总是从0到array.length-1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM