我如何 return 来自方法的数组

How can I return an array from a method

我正在处理一个项目,我需要读取所有文件信息并将其保存到数组中,现在我想 return 数组之一,即客户 ID,以便我可以添加到另一个数组中搜索位置的方法。

public static int[] booking(int dis) { //dis is in the main method to seanner user input
        
        Scanner date = new Scanner(System.in);
        String inputDate;
        if(dis==1) {
            System.out.println("Input the date for the booking");
             inputDate = date.next();
             String str;
             String[] tokens = null;
             int num;
             int line=0;
             int code;
             Booking[] booking = new Booking[20];
             File f = new File("bookings-"+inputDate+".txt");
             try {
                Scanner file = new Scanner(f); //scanner booking file
                
                while(file.hasNext()) {
                    str=file.nextLine();
                    tokens=str.split(",");              
                    booking[line] = new Booking();  // save all values in booking class
                    booking[line].setBookingId(Integer.parseInt(tokens[0]));
                    booking[line].setCustomerID(Integer.parseInt(tokens[1]));
                    booking[line].setBookingDate(tokens[2]);
                    for(int i=0;i<tokens[3].length();i++) { //some value in null 
                        if(tokens[3]==null) {
                            tokens[3]="0";
                            }else booking[line].setTotalPrice(Float.parseFloat(tokens[3]));
                    }
                    num=tokens.length-4;
                    int[] intArray = new int[num];
                    for(int i = 0; i < num; i++) {
                        
                        intArray[i] = Integer.parseInt(tokens[4+i]);
                        
                    }
                    booking[line].setServiceCodes(intArray);
                    line=line+1;
                    
                }
                for(int i=0;i<line;i++) { // the print is works
                //System.out.println(booking[i].getBookingId()+"\t\t"+booking[i].getCustomerID()+"\t\t"+booking[i].getBookingDate()+"\t\t"+booking[i].getTotalPrice()+"\t\t"+Arrays.toString(booking[i].getServiceCodes()));
                    System.out.println(booking[i].getCustomerID());
                    
                }
                
            } catch (FileNotFoundException e) {
                
                e.printStackTrace();
            }
             
            
        }
        return booking; // the return booking is does not work
        
        
    }

如何调用方法和return我想要的数组?

在您的方法签名中,您指定该方法必须 return 一个 int[]。您正在尝试 return booking,类型为 Booking[]。您可以将方法签名更改为 return 输入 Booking[] 代替:

public static Booking[] booking(int dis) {
    ...
}

此外(如 OH-GOD-SPIDERS 所述),booking 变量未在您在 return 语句中使用它的地方定义。您可以通过将 Booking[] booking = new Booking[20]; 移动到 if 语句上方来解决此问题,或者将 return 语句移动到 if 语句中(为 else 添加另一个 return 语句案件)。在这些情况下,您要选择哪种解决方案取决于您希望该方法return。

替换方法签名

public static int[] booking(int dis)

public static Booking[] booking(int dis)

当方法的 return 类型是 int 数组时,您不能将函数 return 设为预订数组 将 int 替换为 booking

声明预订时方法内的行。预订是一种预订方式。

您不能在方法内部实例化方法,因此我建议您将 属性 booking 转换为 int[] 类型,如下所示:

     int[] booking = new int[20];

在预订方法中,您可以return预订,因为您的方法是 int 数组类型

booking 需要声明为 int[],而不是 Booking[]。或者您需要将 Booking[] 声明为 return 语句。