java - java中使用数组调用方法

标签 java

我试图在我的 main 中调用排序方法,但我不知道如何正确执行它。

public static void sortVehicles(Vehicle[] vehicles)
{
    Arrays.sort(vehicles);
    for(int i = 0; i < vehicles.length; i++)
    {
        System.out.println(vehicles[i]);
    }
    return;
}

我正在做这个......

public class 
{
    public static void main(String[] args) throws Exception {
        Vehicle[] vehicles = fillArray(args[0]);

        vehicles = fillArray(args[1]);

        System.out.print("Enter 1 to print all of the files.\nEnter 2 to sort the files by email and print.\nEnter 3 to print the amount of files.\nEnter 4 to print only bike and truck files.\nEnter 5 to print only area codes (987).\nEnter number: ");

        Scanner input = new Scanner(System.in);

        int x = input.nextInt();

        while (true) {
            if (x == 1) {
                printAll(vehicles);
            } else if (x == 2) {
                sortVehicles(vehicles);
            } else if (x == 3) {
                printRecordNumber(vehicles);
            } else if (x == 4) {
                printBicyclesAndTrucks(vehicles);
            } else if (x == 5) {
                printByAreaCode(vehicles);
            }
        }

    }// end main

它给了我一个错误,说它无法识别 sortVehicle 的符号

最佳答案

如果这两个函数不在同一个类中(或者声明 sortVehicles 的类不是声明此 main 方法的父类(super class)) ,在调用 sortVehicles 方法时,您需要指定声明 sortVehicles 的类的名称,因此:

VehicleSorter.sortVehicles(vehicles)

或者,您可以使用 main 方法在类中静态导入 sortVehicles 方法:

import static com.example.VehicleSorter.sortVehicles;

关于java - java中使用数组调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19458593/

相关文章:

java - IBM 案例管理器 : Intermittantly getting "Update sequence number mismatch" trying to save case

java - 布局 xml 中的资源,但编译器错误为 "not found"

java - 如何从jobject中获取jstring? C++

java - 从文件中读取,获取所有空值

java - 正则表达式不应该发生灾难性的回溯

java - 是否有 JDK 或 Guava 方法可以将 null 转换为空列表?

java - 我希望在单击 TextView 时出现的对话框中的 TextView 中显示数字

java - 如何将数据传递给作为 MethodInvokingJobDetailFactoryBean 运行的方法?

java - 为葡萄牙(巴西)获取错误的语言环境

java - 访问限制 : The constructor Provider() is not accessible due to restriction on required library jdk1. 6.0\jre\lib\jsse.jar?