0

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];
    }
}

1 Answer 1

2

Replace

(ObjectContainer[]) new Object[initialCapacity];

by

new ObjectContainer[initialCapacity];

as Object cannot be cast to ObjectContainer

EDIT 1 : With provided class, the following code does compile

public class Main {

    public static void main (String args[]) {
        ObjectContainer[] containers = new ObjectContainer[3];
    }
}

class ObjectContainer extends Object {
    String object;

    private ObjectContainer(String object) {
        this.object = object;
    }
}

EDIT 2 : The following code does compile

public class Main {

    public static void main (String args[]) {
        ObjectContainer[] containers = new ObjectContainer[3];
    }
}

class ObjectContainer extends Object {
    String object;

    private ObjectContainer(String object) {
        this.object = object;
    }
}

interface MultiSet<T> {}
class LinkedMultiHashSet<T> implements MultiSet<T>, Iterable<T> {
    private ObjectContainer hashTable[];
    public LinkedMultiHashSet(int initialCapacity) {
        hashTable = new ObjectContainer[initialCapacity];
    }

    @Override
    public Iterator<T> iterator() {
        return null;
    }
}
Sign up to request clarification or add additional context in comments.

12 Comments

I have tried that but it gives me an error "Generic Array creation" and does not let me perform that
I think that post might answer : stackoverflow.com/questions/3865946/…
The post tells me to Cast an Object into ObjectContainer, which results in my original error listed before
You should provide ObjectContainer class (or library providing that class)
You should specifiy type in diamond operator during initialization. Without ObjectContainer class in the post, it's hard to determine the source of your problem
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.