vehicleList 数组未排序并收到 运行 时间错误
vehicleList array not sorting and receiving a run time error
我的主要方法 运行 出现错误,我无法弄清楚我哪里出了问题。我看到的错误是:
Exception in thread "main" java.lang.ClassCastException: Car cannot be cast to java.base/java.lang.Comparable.
调用 listByOwner
方法时发生错误,Arrays.sort(vehicleList)
。我不相信我有两个 vehicleList 数组,因为我知道这可能会导致对我的 vehicleList 数组进行排序时出现问题。
我的代码:
import java.util.Arrays;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.text.DecimalFormat;
public class UseTaxList {
private String taxDistrict;
private Vehicle[] vehicleList;
private String[] excludedRecords;
public UseTaxList() {
taxDistrict = "not yet assigned";
vehicleList = new Vehicle[0];
excludedRecords = new String[0];
}
public void readVehicleFile(String fileNameIn)
throws FileNotFoundException {
Scanner scanner = new Scanner(new File(fileNameIn));
taxDistrict = scanner.nextLine().trim();
while (scanner.hasNext()) {
String s = scanner.nextLine().trim();
Scanner lineScanner = new Scanner(s);
lineScanner.useDelimiter(";");
char vehicleType = lineScanner.next().toUpperCase().trim().charAt(0);
String owner = lineScanner.next().trim();
String yearMakeModel = lineScanner.next().trim();
String v = lineScanner.next().trim();
String aF = lineScanner.next().trim();
switch (vehicleType) {
case 'C':
double carValue = Double.parseDouble(v);
boolean carAltFuel = Boolean.parseBoolean(aF);
Car newCar = new Car(owner, yearMakeModel, carValue, carAltFuel);
addVehicle(newCar);
break;
case 'T':
double truckValue = Double.parseDouble(v);
boolean truckAltFuel = Boolean.parseBoolean(aF);
String t = lineScanner.next().trim();
double tons = Double.parseDouble(t);
Truck newTruck = new Truck(owner, yearMakeModel, truckValue,
truckAltFuel, tons);
addVehicle(newTruck);
break;
case 'S':
double semiValue = Double.parseDouble(v);
boolean semiAltFuel = Boolean.parseBoolean(aF);
String st = lineScanner.next().trim();
double semiTons = Double.parseDouble(st);
String ax = lineScanner.next().trim();
int axle = Integer.parseInt(ax);
SemiTractorTrailer newSemi = new SemiTractorTrailer(owner,
yearMakeModel, semiValue, semiAltFuel, semiTons, axle);
addVehicle(newSemi);
break;
case 'M':
String e = lineScanner.next().trim();
double engSize = Double.parseDouble(e);
double mValue = Double.parseDouble(v);
boolean mAltFuel = Boolean.parseBoolean(aF);
Motorcycle newMotorcycle = new Motorcycle(owner,
yearMakeModel, mValue, mAltFuel, engSize);
addVehicle(newMotorcycle);
break;
default:
addExcludedRecords("Excluded Records:\n" + s);
break; }
}
}
public String getTaxDistrict() {
return taxDistrict;
}
public void setTaxDistrict(String taxDistrictIn) {
taxDistrict = taxDistrictIn;
}
public Vehicle[] getVehicleList() {
return vehicleList;
}
public String[] getExcludedRecords() {
return excludedRecords;
}
public void addVehicle(Vehicle vehicleIn) {
vehicleList = Arrays.copyOf(vehicleList, vehicleList.length + 1);
vehicleList[vehicleList.length - 1] = vehicleIn;
}
public void addExcludedRecords(String recordsIn) {
excludedRecords = Arrays.copyOf(excludedRecords,
excludedRecords.length + 1);
excludedRecords[excludedRecords.length - 1] = recordsIn;
}
public String toString() {
String output = "";
for (Vehicle v1 : vehicleList) {
output += "\n" + v1 + "\n"; }
return output;
}
public double calculateTotalUseTax() {
double total = 0.0;
for (Vehicle v1 : vehicleList) {
total += v1.useTax();
}
return total;
}
public double calculateTotalValue() {
double total = 0.0;
for (Vehicle v1 : vehicleList) {
total += v1.getValue();
}
return total;
}
public String summary() {
DecimalFormat formatter = new DecimalFormat("##,###.00");
String output = "";
output += "------------------------------\n";
output += "Summary for " + taxDistrict + "\n";
output += "------------------------------";
output += "\nNumber of Vehicles: " + vehicleList.length;
output += "\nTotal Value: $" + formatter.format(calculateTotalValue());
output += "\nTotal Use Tax: $" + formatter.format(calculateTotalUseTax())
+ "\n";
return output;
}
public String listByOwner() {
Arrays.sort(vehicleList);
String output = "------------------------------\n";
output += "Vehicles by Owner\n";
output += "------------------------------\n";
for (Vehicle v1 : vehicleList) {
output += "\n" + v1 + "\n";
}
return output;
}
public String listByUseTax() {
Arrays.sort(vehicleList, new UseTaxComparator());
String output = "------------------------------\n";
output += "Vehicles by Use Tax\n";
output += "------------------------------\n";
for (Vehicle v1 : vehicleList) {
output += "\n" + v1 + "\n";
}
return output;
}
public String excludedRecordsList() {
String output = "";
output += "------------------------------\n";
output += "Excluded Records\n";
output += "------------------------------\n";
for (String v1 : excludedRecords) {
output += "\n" + v1 + "\n";
}
return output;
}
}
根据您遇到的错误,您的 Vehicle
class 未实现 Comparable
接口,因此您必须显式传递 Comparator
到 sort
调用:
Arrays.sort(vehicleList, Comparator.comparing(Vehicle::getOwner));
看来您必须在 Car
class 中实现 Comparable
接口才能使 Arrays.sort
工作...如果您不这样做,排序就不会知道什么时候 Car
比其他车 "higher"。
我的主要方法 运行 出现错误,我无法弄清楚我哪里出了问题。我看到的错误是:
Exception in thread "main" java.lang.ClassCastException: Car cannot be cast to java.base/java.lang.Comparable.
调用 listByOwner
方法时发生错误,Arrays.sort(vehicleList)
。我不相信我有两个 vehicleList 数组,因为我知道这可能会导致对我的 vehicleList 数组进行排序时出现问题。
我的代码:
import java.util.Arrays;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.text.DecimalFormat;
public class UseTaxList {
private String taxDistrict;
private Vehicle[] vehicleList;
private String[] excludedRecords;
public UseTaxList() {
taxDistrict = "not yet assigned";
vehicleList = new Vehicle[0];
excludedRecords = new String[0];
}
public void readVehicleFile(String fileNameIn)
throws FileNotFoundException {
Scanner scanner = new Scanner(new File(fileNameIn));
taxDistrict = scanner.nextLine().trim();
while (scanner.hasNext()) {
String s = scanner.nextLine().trim();
Scanner lineScanner = new Scanner(s);
lineScanner.useDelimiter(";");
char vehicleType = lineScanner.next().toUpperCase().trim().charAt(0);
String owner = lineScanner.next().trim();
String yearMakeModel = lineScanner.next().trim();
String v = lineScanner.next().trim();
String aF = lineScanner.next().trim();
switch (vehicleType) {
case 'C':
double carValue = Double.parseDouble(v);
boolean carAltFuel = Boolean.parseBoolean(aF);
Car newCar = new Car(owner, yearMakeModel, carValue, carAltFuel);
addVehicle(newCar);
break;
case 'T':
double truckValue = Double.parseDouble(v);
boolean truckAltFuel = Boolean.parseBoolean(aF);
String t = lineScanner.next().trim();
double tons = Double.parseDouble(t);
Truck newTruck = new Truck(owner, yearMakeModel, truckValue,
truckAltFuel, tons);
addVehicle(newTruck);
break;
case 'S':
double semiValue = Double.parseDouble(v);
boolean semiAltFuel = Boolean.parseBoolean(aF);
String st = lineScanner.next().trim();
double semiTons = Double.parseDouble(st);
String ax = lineScanner.next().trim();
int axle = Integer.parseInt(ax);
SemiTractorTrailer newSemi = new SemiTractorTrailer(owner,
yearMakeModel, semiValue, semiAltFuel, semiTons, axle);
addVehicle(newSemi);
break;
case 'M':
String e = lineScanner.next().trim();
double engSize = Double.parseDouble(e);
double mValue = Double.parseDouble(v);
boolean mAltFuel = Boolean.parseBoolean(aF);
Motorcycle newMotorcycle = new Motorcycle(owner,
yearMakeModel, mValue, mAltFuel, engSize);
addVehicle(newMotorcycle);
break;
default:
addExcludedRecords("Excluded Records:\n" + s);
break; }
}
}
public String getTaxDistrict() {
return taxDistrict;
}
public void setTaxDistrict(String taxDistrictIn) {
taxDistrict = taxDistrictIn;
}
public Vehicle[] getVehicleList() {
return vehicleList;
}
public String[] getExcludedRecords() {
return excludedRecords;
}
public void addVehicle(Vehicle vehicleIn) {
vehicleList = Arrays.copyOf(vehicleList, vehicleList.length + 1);
vehicleList[vehicleList.length - 1] = vehicleIn;
}
public void addExcludedRecords(String recordsIn) {
excludedRecords = Arrays.copyOf(excludedRecords,
excludedRecords.length + 1);
excludedRecords[excludedRecords.length - 1] = recordsIn;
}
public String toString() {
String output = "";
for (Vehicle v1 : vehicleList) {
output += "\n" + v1 + "\n"; }
return output;
}
public double calculateTotalUseTax() {
double total = 0.0;
for (Vehicle v1 : vehicleList) {
total += v1.useTax();
}
return total;
}
public double calculateTotalValue() {
double total = 0.0;
for (Vehicle v1 : vehicleList) {
total += v1.getValue();
}
return total;
}
public String summary() {
DecimalFormat formatter = new DecimalFormat("##,###.00");
String output = "";
output += "------------------------------\n";
output += "Summary for " + taxDistrict + "\n";
output += "------------------------------";
output += "\nNumber of Vehicles: " + vehicleList.length;
output += "\nTotal Value: $" + formatter.format(calculateTotalValue());
output += "\nTotal Use Tax: $" + formatter.format(calculateTotalUseTax())
+ "\n";
return output;
}
public String listByOwner() {
Arrays.sort(vehicleList);
String output = "------------------------------\n";
output += "Vehicles by Owner\n";
output += "------------------------------\n";
for (Vehicle v1 : vehicleList) {
output += "\n" + v1 + "\n";
}
return output;
}
public String listByUseTax() {
Arrays.sort(vehicleList, new UseTaxComparator());
String output = "------------------------------\n";
output += "Vehicles by Use Tax\n";
output += "------------------------------\n";
for (Vehicle v1 : vehicleList) {
output += "\n" + v1 + "\n";
}
return output;
}
public String excludedRecordsList() {
String output = "";
output += "------------------------------\n";
output += "Excluded Records\n";
output += "------------------------------\n";
for (String v1 : excludedRecords) {
output += "\n" + v1 + "\n";
}
return output;
}
}
根据您遇到的错误,您的 Vehicle
class 未实现 Comparable
接口,因此您必须显式传递 Comparator
到 sort
调用:
Arrays.sort(vehicleList, Comparator.comparing(Vehicle::getOwner));
看来您必须在 Car
class 中实现 Comparable
接口才能使 Arrays.sort
工作...如果您不这样做,排序就不会知道什么时候 Car
比其他车 "higher"。