在java中使用数组调用方法

在java中使用数组调用方法,java,Java,我试图在我的main中调用sort方法,但我不知道如何正确地执行它 public static void sortVehicles(Vehicle[] vehicles) { Arrays.sort(vehicles); for(int i = 0; i < vehicles.length; i++) { System.out.println(vehicles[i]); } return; } 它给我一个错误,说它无法识别sortV

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

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

它给我一个错误,说它无法识别sortVehicle的符号。或者,您可以在
车辆
类中设置main

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;
}

}
公共级车辆
{
公共静态void main(字符串[]args)引发异常{
Vehicle[]vehicles=fillArray(args[0]);
车辆=填充阵列(参数[1]);
System.out.print(“输入1打印所有文件。\n输入2通过电子邮件对文件进行排序并打印。\n输入3打印文件量。\n输入4只打印自行车和卡车文件。\n输入5只打印区号(987)。\n输入编号:”;
扫描仪输入=新扫描仪(System.in);
int x=input.nextInt();
while(true){
如果(x==1){
打印所有(车辆);
}else如果(x==2){
货车(车辆);
}else如果(x==3){
打印记录编号(车辆);
}else如果(x==4){
印刷自行车和卡车(车辆);
}else如果(x==5){
打印区域代码(车辆);
}
}
}//端干管
公共车辆(车辆[]辆)
{
数组。排序(车辆);
对于(int i=0;i
或者,您可以在
车辆
类中设置main

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;
}

}
公共级车辆
{
公共静态void main(字符串[]args)引发异常{
Vehicle[]vehicles=fillArray(args[0]);
车辆=填充阵列(参数[1]);
System.out.print(“输入1打印所有文件。\n输入2通过电子邮件对文件进行排序并打印。\n输入3打印文件量。\n输入4只打印自行车和卡车文件。\n输入5只打印区号(987)。\n输入编号:”;
扫描仪输入=新扫描仪(System.in);
int x=input.nextInt();
while(true){
如果(x==1){
打印所有(车辆);
}else如果(x==2){
货车(车辆);
}else如果(x==3){
打印记录编号(车辆);
}else如果(x==4){
印刷自行车和卡车(车辆);
}else如果(x==5){
打印区域代码(车辆);
}
}
}//端干管
公共车辆(车辆[]辆)
{
数组。排序(车辆);
对于(int i=0;i
如果这两个函数不在同一个类中(或者声明
sortVehicles
的类不是声明此
main
方法的类的超类),则在调用
sortVehicles
方法时,需要指定声明
sortVehicles
的类的名称,因此:

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

import static com.example.VehicleSorter.sortVehicles;

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

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

import static com.example.VehicleSorter.sortVehicles;
  • 静态方法应使用
    调用,例如
    Math.max(int,int)
因此,在您的案例中,应该是:

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

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

    公共课 { 公共静态void main(字符串[]args)引发异常{ Vehicle[]vehicles=fillArray(args[0])

    vehicles=fillArray(args[1]);
    System.out.print(“输入1打印所有文件。\n输入2通过电子邮件对文件进行排序并打印。\n输入3打印文件量。\n输入4只打印自行车和卡车文件。\n输入5只打印区号(987)。\n输入编号:”;
    扫描仪输入=新扫描仪(System.in);
    int x=input.nextInt();
    while(true){
    如果(x==1){
    打印所有(车辆);
    }else如果(x==2){
    货车(车辆);
    }else如果(x==3){
    打印记录编号(车辆);
    }else如果(x==4){
    印刷自行车和卡车(车辆);
    }else如果(x==5){
    打印区域代码(车辆);
    }
    }
    }
    公共车辆(车辆[]辆)
    {
    数组。排序(车辆);
    对于(int i=0;i
      • 静态方法应使用
        调用,例如
        Math.max(int,int)
      因此,在您的案例中,应该是:

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

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

        公共课 { 公共静态void main(字符串[]args)引发异常{ Vehicle[]vehicles=fillArray(args[0])

        vehicles=fillArray(args[1]);
        System.out.print(“输入1打印所有文件。\n输入2通过电子邮件对文件进行排序并打印。\n输入3打印文件量。\n输入4只打印自行车和卡车文件。\n输入5只打印区号(987)。\n输入编号:”;
        扫描仪输入=新扫描仪(System.in);
        int x=input.nextInt();
        while(true){
        如果(x==1){
        打印所有(车辆);
        }else如果(x==2){
        货车(车辆);
        }else如果(x==3){
        打印记录编号(车辆);
        }else如果(x==4){
        印刷自行车和卡车(车辆);
        }else if(x)