I have created a custom class called ObjectContainer, and am trying to create an array containing those objects. However, when i try to instantiate the array with new ObjectContainer[initialCapacity];
it gives me Generic Array creation error. I thus found an answer online that suggests casting an object to it, which i have done
(ObjectContainer[]) new Object[initialCapacity];
However, i now get another error
java.lang.ClassCastException: class [Ljava.lang.Object; cannot be cast to class [LHashSet$ObjectContainer;
([Ljava.lang.Object; is in module java.base of loader 'bootstrap';
[LHashSet$ObjectContainer; is in unnamed module of loader 'app')
What do i do to create an ObjectContainer array?
Edit: Using
(ObjectContainer[]) new ObjectContainer[initialCapacity];
Gives me Generic array creation error.
ObjectContainer class:
private class ObjectContainer extends Object {
String object;
private ObjectContainer(String object) {
this.object = object;
}
This is implemented within a HashSet class:
public class HashSet<T> implements MultiSet<T>, Iterable<T> {
private ObjectContainer hashTable[];
public LinkedMultiHashSet(int initialCapacity) {
hashTable = (ObjectContainer[]) new ObjectContainer[initialCapacity];
}
}