在Java中使用泛型时出现了ClassCastException。

huangapple go评论72阅读模式
英文:

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 &quot;main&quot; 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&lt;Car&gt; {

    	private static ParkingLot&lt;Car&gt; park=new ParkingLot&lt;Car&gt;(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&lt;T&gt; {
	
	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 &lt;T extends Vehicle&gt; boolean deleteVehicle(T v) {
		for(int i=0;i&lt;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&lt;Car&gt; v=new CarDAOImpl();
		boolean inserted=v.insert(new Car(1,&quot;ford&quot;,&quot;Red&quot;,1234));
		System.out.println(inserted);
		Car[] c=v.getAll();
		for(int i=0;i&lt;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 &quot;main&quot; 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&lt;T&gt; clazz, int sp) {
    this.vehicle= (T[]) Array.newInstance(clazz, sp);
    this.space=sp;
}

And your variable declaration should look like this:

private static ParkingLot&lt;Car&gt; park = new ParkingLot&lt;&gt;(Car.class, 10);

huangapple
  • 本文由 发表于 2020年10月7日 20:39:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/64244230.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定