英文:
ClassCastException while using generics in java
问题
我是新来的 Java 编程者,正在学习泛型。我尝试自己编写了一些泛型程序,但是遇到了以下异常:
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Lcom.ashwin.model.Car;
我有一个 Vehicle 类:
public class Vehicle {
private int id;
private String name;
private String color;
private int plateNo;
// 省略了获取器和设置器
}
我有一个继承自 Vehicle 类的 Car 类:
public class Car extends Vehicle {
public Car(int id, String name, String color, int plateNo) {
super.setId(id);
super.setColor(color);
super.setPlateNo(plateNo);
}
}
我有一个 CarDAOImpl.java 类:
public class CarDAOImpl implements VehicleDAO<Car> {
private static ParkingLot<Car> park = new ParkingLot<Car>(10);
@Override
public boolean insert(Car v) {
if (park.getSpace() == -1) {
return false;
} else {
park.setSpace(park.getSpace() - 1);
park.setVehicle(v);
return true;
}
}
@Override
public boolean delete(Car k) {
if (park.getSpace() == 10) {
return false;
} else {
boolean result = park.deleteVehicle(k);
return result;
}
}
@Override
public Car[] getAll() {
return park.getVehicle();
}
}
我还有一个 ParkingLot.java 类:
public class ParkingLot<T> {
private int space;
private T[] vehicle;
public ParkingLot() {
}
public ParkingLot(int sp) {
this.vehicle = (T[]) new Object[sp];
this.space = sp;
}
public int getSpace() {
return space;
}
public void setSpace(int space) {
this.space = space;
}
public T[] getVehicle() {
return vehicle;
}
public void setVehicle(T vehicle) {
this.vehicle[space] = vehicle;
}
public <T extends Vehicle> boolean deleteVehicle(T v) {
for (int i = 0; i < vehicle.length; i++) {
if (((Vehicle) vehicle[i]).getId() == v.getId()) {
vehicle[i] = null;
return true;
}
}
return false;
}
}
我的 main 方法是:
public class Main {
public static void main(String[] args) {
VehicleDAO<Car> v = new CarDAOImpl();
boolean inserted = v.insert(new Car(1, "ford", "Red", 1234));
System.out.println(inserted);
Car[] c = v.getAll();
for (int i = 0; i < c.length; i++) {
System.out.println(c[i]);
}
}
}
在 CarDAOImpl.java 类的以下行出现了错误:
@Override
public Car[] getAll() {
return park.getVehicle();
}
英文:
I am new to java programming and I am learning generics.I tried to do some generics program by myself and I am getting Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Lcom.ashwin.model.Car;
.
I have a Vehicle Class:
public class Vehicle {
private int id;
private String name;
private String color;
private int plateNo;
//omitted getters and setters
}
I have a Car class extending Vehicle Class.
public class Car extends Vehicle {
public Car(int id, String name, String color, int plateNo) {
super.setId(id);
super.setColor(color);
super.setPlateNo(plateNo);
}
}
I have CarDAOImpl.java class:
public class CarDAOImpl implements VehicleDAO<Car> {
private static ParkingLot<Car> park=new ParkingLot<Car>(10);
@Override
public boolean insert(Car v) {
if(park.getSpace()==-1) {
return false;
}
else {
park.setSpace(park.getSpace()-1);
park.setVehicle(v);
return true;
}
}
@Override
public boolean delete(Car k) {
if(park.getSpace()==10) {
return false;
}
else {
boolean result=park.deleteVehicle(k);
return result;
}
}
@Override
public Car[] getAll() {
return park.getVehicle();
}
}
I have another ParkingLot.java class:
public class ParkingLot<T> {
private int space;
private T[] vehicle;
public ParkingLot() {
}
public ParkingLot(int sp) {
this.vehicle=(T[])new Object[sp];
this.space=sp;
}
public int getSpace() {
return space;
}
public void setSpace(int space) {
this.space = space;
}
public T[] getVehicle() {
return vehicle;
}
public void setVehicle(T vehicle) {
this.vehicle[space]=vehicle;
}
public <T extends Vehicle> boolean deleteVehicle(T v) {
for(int i=0;i<vehicle.length;i++) {
if(((Vehicle) vehicle[i]).getId()==v.getId()) {
vehicle[i]=null;
return true;
}
}
return false;
}
}
My main method is:
public class Main {
public static void main(String[] args) {
VehicleDAO<Car> v=new CarDAOImpl();
boolean inserted=v.insert(new Car(1,"ford","Red",1234));
System.out.println(inserted);
Car[] c=v.getAll();
for(int i=0;i<c.length;i++)
{
System.out.println(c[i]);
}
}
}
I am getting error at this line of CarDAOImpl.java class:
@Override
public Car[] getAll() {
return park.getVehicle();
}
The exception is:
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Lcom.ashwin.model.Car;
答案1
得分: 1
你需要更新你的构造函数,将类对象作为参数包含进去:
public ParkingLot(Class<T> clazz, int sp) {
this.vehicle = (T[]) Array.newInstance(clazz, sp);
this.space = sp;
}
你的变量声明应该像这样:
private static ParkingLot<Car> park = new ParkingLot<>(Car.class, 10);
英文:
You need to update your constructor to include the class object as a parameter:
public ParkingLot(Class<T> clazz, int sp) {
this.vehicle= (T[]) Array.newInstance(clazz, sp);
this.space=sp;
}
And your variable declaration should look like this:
private static ParkingLot<Car> park = new ParkingLot<>(Car.class, 10);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论