我有一个菜单系统,可以打印有关 ArrayList 的各种信息。在我的第三种情况下,我必须调用一个打印数组列表大小的方法。我试图调用的方法是同一类的一部分。这些是我收到的错误消息。我将如何解决这些问题?
错误:
non-static method printrecordnumber(ArrayList<vehicle>) cannot be referenced from a static context
case 3: System.out.println("Number of records: " + printrecordnumber(vehiclearray));
错误:
'void' type not allowed here
case 3: System.out.println("Number of records: " + printrecordnumber(vehiclearray));
这是我的代码:
//Menu System
do{
System.out.println("Enter number of option or stop to stop the program\n1. Print all data\n2. Print all data (Sorted)\n3. Print number of records\n4. Print Bicycles and Trucks from the sorted data\n5. Print vehicles from area code 987");
try{
String option = input.next();
if((option.equals("stop"))) {
System.exit(0);
}
int optionint = Integer.parseInt(option);
switch (optionint){
case 1:
break;
case 2:
break;
case 3:
System.out.println("Number of records: " + printrecordnumber(vehiclearray));
break;
case 4:
break;
case 5:
break;
}
}
catch (NumberFormatException e){}
}while(true);
//Main Method Ending Bracket
}
public void printrecordnumber(ArrayList<vehicle> vehiclearray){
System.out.println(vehiclearray.size());
}