Im a bit beginner. Funny or not, I dont know how to create an empty typed array. My class:
class Depot
{
}
storing them:
private final HashMap<Integer, Depot> depots;
depots = new HashMap<>();
and I want to return them:
public Depot[] getDepots()
{
if (depots.values().isEmpty())
{
return (Depot[]) new Object[0]; ***
}
else
{
return depots.values().toArray(new Depot[depots.values().size()-1]);
}
}
as an array. Now when there is no depots, them comes the tricky part. First of all, this line depots.values().size()-1 fails. The return (Depot[]) new Object[0]; seems to be OK, but still triggers an exception: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Depot;
new Depot[0]?