I have a generic class, SimpleStack<T>
, which contains these methods:
private T[] stack;
public T[] getStack() {
return this.stack;
}
public boolean add(T item) {
boolean filled = isFull();
if (!filled) {
stack[size()] = item;
return true;
}
else {
return false;
}
}
public int size() {
int count = 0;
for (T item : stack) {
if (item != null) {
count++;
}
}
return count;
}
I'm trying to use this class to act as a Profile
class, and I've filled the array stack
with Profile
objects. However, when I try to return this array, it says that it can't cast from Object
to Profile
.
Here's a snippet from where I fill the array, inside my main method:
SimpleStack<Profile> profiles = new SimpleStack<Profile>(50);
Profile profile = new Profile();
profiles.add(profile);
I try to call the getter to return the array, and cast it to a Profile[]
. This is what fails:
Profile[] tempStack = Arrays.copyOf(profiles.getStack(), (profiles.getStack()).length, Profile[].class);
I've tried it several ways, but the ultimately all do the same thing (attempt to cast the generic array to Profile[]
).
Does anyone know what I've done wrong? I can provide more code if necessary. I tried to take the relevant bits.