在java中使用数组调用方法

时间:2013-10-18 20:34:10

标签: java

我试图在我的主要调用sort方法,但我无法弄清楚如何正确地执行它。

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的符号

3 个答案:

答案 0 :(得分:4)

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

VehicleSorter.sortVehicles(vehicles)

或者,您可以使用sortVehicles方法静态导入班级中的main方法:

import static com.example.VehicleSorter.sortVehicles;

答案 1 :(得分:0)

或者,您可以在Vehicle课程中修改主要内容。

public class Vehicle
{
    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

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

}

答案 2 :(得分:0)

  • 应使用 <class name>.<function name> 调用静态方法,例如Math.max(int , int)

所以在你的情况下应该是:

<Class name to which sortVehicles belongs>.sortVehicles(vehicles)

  • 否则将静态方法放在main方法所属的类中:

    公共课 {     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);
            }
        }
    
    }
    public static void sortVehicles(Vehicle[] vehicles)
    {
        Arrays.sort(vehicles);
        for(int i = 0; i < vehicles.length; i++)
        {
             System.out.println(vehicles[i]);
        }
        return;
    }
    
    }