Java 使用foreach循环查找数组中的最大元素

Java 使用foreach循环查找数组中的最大元素,java,arrays,Java,Arrays,我读了一篇关于foreach循环的文章,试图找到数组中最大的元素,但它没有像通常的那样工作。我想知道有什么区别 public void foreachloop() { int[] T = { 1, 2, 3, 4, 5, 6, 7 }; int x = T[0]; for (int element : T) { if (T[element] > x) { x = T[element]; } }

我读了一篇关于foreach循环的文章,试图找到数组中最大的元素,但它没有像通常的那样工作。我想知道有什么区别

public void foreachloop()
{
    int[] T = { 1, 2, 3, 4, 5, 6, 7 };
    int x = T[0];

    for (int element : T) {
        if (T[element] > x) {
            x = T[element];
        }
    }

    System.out.println(x);
}
对(int-element:T)执行
操作时,变量
element
将遍历
T
中的每个值。因此,您不应该试图查看值
t[element]
——只查看
element
本身

像这样:

for (int element: T) {
    if (element > x) x = element;
}
如果在
element
为7时尝试访问
T[element]
,则会引发异常,因为7不是数组的有效索引。(有效索引为0到6。)

当对(int-element:T)
执行
操作时,变量
element
将遍历
T
中的每个值。因此,您不应该试图查看值
t[element]
——只查看
element
本身

像这样:

for (int element: T) {
    if (element > x) x = element;
}

如果在
element
为7时尝试访问
T[element]
,则会引发异常,因为7不是数组的有效索引。(有效索引为0到6。)

使用foreach时,首先定义数组的类型(这里是
int
),然后给出任意名称(选择
元素
),然后是
,然后是数组的名称。之后,您可以使用选择的名称访问元素

public void foreachloop()
{
    int[] T = { 1, 2, 3, 4, 5, 6, 7 };
    int x = T[0];

    for (int element : T) {
        if (element > x) {
            x = element;
        }
    }

    System.out.println(x);
}

使用foreach时,首先定义数组的类型(这里是
int
),然后给出任意名称(选择
元素
),然后是
,然后是数组的名称。之后,您可以使用选择的名称访问元素

public void foreachloop()
{
    int[] T = { 1, 2, 3, 4, 5, 6, 7 };
    int x = T[0];

    for (int element : T) {
        if (element > x) {
            x = element;
        }
    }

    System.out.println(x);
}
  • max
    初始化为可能的最小整数
  • 使用for each循环检查当前元素是否大于
    max
    ,如果大于,则使新值
    max
  • 完成for each循环后返回
    max
  • 就我个人而言,我将为此创建一个单独的方法:

    import java.util.Arrays;
    
    class Main {
      public static void main(String[] args) {
        int[] arr = new int[]{1,2,3};
        System.out.println("The array looks like: " + Arrays.toString(arr)); //The array looks like: [1, 2, 3]
        System.out.println("The max of the array is: " + arrayMax(arr)); //The max of the array is: 3
      }
    
      public static int arrayMax (int[] arr) {
        int max = Integer.MIN_VALUE; //initilize max to the smallest integer possible
        for(int element: arr)
            max = Math.max(max, element);
        return max;
      }
    }
    
    试试看

  • max
    初始化为可能的最小整数
  • 使用for each循环检查当前元素是否大于
    max
    ,如果大于,则使新值
    max
  • 完成for each循环后返回
    max
  • 就我个人而言,我将为此创建一个单独的方法:

    import java.util.Arrays;
    
    class Main {
      public static void main(String[] args) {
        int[] arr = new int[]{1,2,3};
        System.out.println("The array looks like: " + Arrays.toString(arr)); //The array looks like: [1, 2, 3]
        System.out.println("The max of the array is: " + arrayMax(arr)); //The max of the array is: 3
      }
    
      public static int arrayMax (int[] arr) {
        int max = Integer.MIN_VALUE; //initilize max to the smallest integer possible
        for(int element: arr)
            max = Math.max(max, element);
        return max;
      }
    }
    

    如果(element>x)x=element,则尝试使用它

    element
    表示数组的整个元素,而不是其索引
    变量名称不要使用单大写字母
    T
    通常用作泛型类型参数。在调试器中单步执行代码应该是解决此问题的最快方法。
    element
    表示数组的整个元素,而不是其索引
    if(element>x)x=element变量名称不要使用单大写字母
    T
    通常用作泛型类型参数。在调试器中单步执行代码应该是解决此问题的最快方法。