首页 > 解决方案 > 为什么在计算整数数组的最小值时会出现“实际或形式参数列表长度不同”的错误?

问题描述

我收到编译时错误

method min in class RandomArray cannot be applied to given types;
        System.out.println(table.min());
                                ^
  required: int[]
  found:    no arguments
  reason: actual and formal argument lists differ in length

但我不明白为什么我得到它?

这是我的代码

import java.util.Scanner;
import java.util.Random;

public class RandomArray {

    private int data[];
    private int value;

    public RandomArray(int x) {
        Random gen = new Random();
        data = new int[x];

        for (int index = 0; index < x; index++)
            data[index] = gen.nextInt(x);
    }

    public String toString() {
        String output = "";
        for (int i = 0; i < data.length; i++) {
            output += data[i];
        }
        return output;
    }

    public static int min(int[] x) {
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < x.length; i++) {
            if (x[i] < min) {
                min = x[i];
            }
        }
        return min;
    }
    
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int x;
        int data;
        System.out.println("please enter the number of integers you would like to create an array for");
        x = scan.nextInt();
        RandomArray table = new RandomArray(x);

        System.out.println(table.toString());
        System.out.println(table.min());
    }
}

标签: javaarraysmethodsmin

解决方案


正如其他人在评论中指出的那样,您会收到此错误

Error:(49, 33) java: method min in class RandomArray cannot be applied to given types;
  required: int[]
  found:    no arguments
  reason: actual and formal argument lists differ in length

因为您在调用声明的public static int min(int[] x)方法时没有传递参数table.min()!因此actual and formal argument lists differ in length

(其中actual指的是调用的方法参数,table.min()长度为 0 的参数列表,以及具有长度为 1 的参数列表formal的声明方法int min(int[] x))。

给定您的代码,您想要传递给您的min方法的参数实际上是您的类的成员变量RandomArray- 数组int data[]。因此,您不应该将任何参数传递给您的min方法,而是让它直接在您的data []数组上操作,如

public int min() {
    int min = Integer.MAX_VALUE;
    for (int i = 0; i < data.length; i++) {
        if (data[i] < min) {
            min = data[i];
        }
    }
    return min;
}

请注意,当您希望类中的方法对非静态成员变量(例如private int data[];数组)进行操作时,您需要使该方法也非static.

(作为风格问题,而不是 C 风格的数组声明int data[]更喜欢 Java 风格的声明int[] data)。

把它们放在一起你得到

import java.util.Arrays;
import java.util.Scanner;
import java.util.Random;

public class RandomArray {

    private int[] data;

    public RandomArray(int x) {
        Random gen = new Random();
        data = new int[x];

        for (int index = 0; index < x; index++) {
            data[index] = gen.nextInt(x);
        }
    }

    public String toString() {
        return Arrays.toString(data);
    }

    public int min() {
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < data.length; i++) {
            if (data[i] < min) {
                min = data[i];
            }
        }
        return min;
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("please enter the number of integers you would like to create an array for");
        int x = scan.nextInt();
        RandomArray table = new RandomArray(x);

        System.out.println(table.toString());
        System.out.println(table.min());
    }
}

给你输出

please enter the number of integers you would like to create an array for
10
[9, 3, 3, 9, 2, 9, 2, 9, 5, 4]
2

推荐阅读