How can I expand an array without knowing its type?
I have an Object[]
, and I know it contains only, say, Car
instances. When I try to typecast it to a Car[]
, it throws a ClassCastException. I'm working in an environment where Generics are not available.
Must I use a for-loop to copy each element manually into a new array like this:
Car[] cars = new Car[objects.length];
for (int i = 0; i < objects.length; i++) {
cars[i] = (Car) objects[i];
}
?