【问题标题】:Collections.max gives me an error in java, can anyone please Assist?Collections.max 在 java 中给了我一个错误,有人可以帮忙吗?
【发布时间】:2021-11-24 18:56:40
【问题描述】:

代码如下:

import java.util.Collections;
import java.util.Scanner;

public class task2 {
  public static void main(String[] args) {

    int n;
    Scanner numbers = new Scanner(System.in);
    System.out.print("Enter the number of elements you want to store: ");
    // reading the number of elements from the that we want to enter
    n = numbers.nextInt();
    // creates an array in the memory of length 10
    int[] array = new int[10];
    System.out.println("Enter the elements of the array: ");
    for (int i = 0; i < n; i++) {
      // reading array elements from the user
      array[i] = numbers.nextInt();
    }
    System.out.println("Array elements are: ");
    // accessing array elements using the for loop
    for (int i = 0; i < n; i++) {
      System.out.println(array[i]);
    }

    System.out.println("The max value is: " + Collections.max(numbers));
  }
}

我收到一条错误消息,提示“集合类型中的方法 max(Collection extends T>) 不适用于参数(扫描器)”

如果有人可以帮忙,那就太好了,请记住,我是 Java 的绝对初学者,所以请原谅我犯的任何愚蠢的错误。

【问题讨论】:

  • numbers 是Scanner。 Scanner 的 max 是什么?
  • 我正在尝试从我在数组中输入的输入中获取最大值
  • @AMalak 那么你需要对数组做一些事情,而不是你用来从输入中读取值的扫描仪。

标签: java arrays collections java.util.scanner


【解决方案1】:
Collections.max(numbers)

不起作用,因为 numbers 是 Scanner。 “Scanner 的最大值”没有意义:它没有最大值或最小值,它一次只给你一个值。

您需要对读取值的数组做一些事情。

你不能做Collections.max(array),因为array 是一个数组,而不是一个集合。您可以使用Arrays.asList(...) 将一些数组(具有引用类型元素的数组,如String[]、Object[] 等)放入集合中;但你不能用array 做到这一点,因为它是int[]。或者更确切地说,您可以使用array 执行此操作,但您会得到一个List&lt;int[]&gt;,您也不能将其传递给Collections.max,因为int[] 没有自然排序.

你可以做的是:

  • 将array 的类型从int[] 更改为Integer[]:然后Collections.max(Arrays.asList(...)) 将起作用;
  • 用List&lt;Integer&gt; list和add替换int[] array而不是设置元素;
  • 请改用IntStream.of(array).max().orElseThrow()。

【讨论】:

    【解决方案2】:

    也许

    public static void main(final String[] args) {
    
        final Scanner numbers = new Scanner(System.in);
        System.out.print("Enter the number of elements you want to store: ");
        // reading the number of elements from the that we want to enter
        final Integer n = numbers.nextInt();
        // creates an array in the memory of length 10
        final List<Integer> array = new ArrayList<>(10);
        System.out.println("Enter the elements of the array: ");
        for (int i = 0; i < n; i++) {
            // reading array elements from the user
            array.set(i, numbers.nextInt());
        }
        System.out.println("Array elements are: ");
        // accessing array elements using the for loop
        for (int i = 0; i < n; i++) {
            System.out.println(array.get(i));
        }
    
        System.out.println("The max value is: " + Collections.max(array));
    }
    

    【讨论】:

      猜你喜欢
      • 2021-07-17
      • 2022-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-09
      • 1970-01-01
      • 2021-08-24
      相关资源
      最近更新 更多