java - 为什么在计算整数数组的最小值时会出现“实际或形式参数列表长度不同”的错误?
问题描述
我收到编译时错误
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());
}
}
解决方案
正如其他人在评论中指出的那样,您会收到此错误
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
推荐阅读
- linux - 在没有 GUI 的 Linux mint 19.3 上运行 Electron 应用程序
- javascript - 基于leftIndent从扁平数组制作树结构
- javascript - 多个 fetch Promises - 相同的 url,不同的正文
- node.js - Discord.js:在类别中创建频道
- java - 如何在房间数据库中应用条件?
- reporting-services - SSRS 2014。在分页符之前显示 RunningValue
- javascript - 使用 Vue.js 在 Safari 中下载文件
- laravel - Laravel Mail::To() 不发送电子邮件但适用于 Mail::Send()
- c++ - rdstate() 函数返回值 3 用于在文件处理 C++ 中使用 eof() 函数的代码
- c++ - c++ - 如何在类中以struct作为数据成员定义构造函数?