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];
    }
}
General Grievance
  • 4,555
  • 31
  • 31
  • 45
kevin chua
  • 23
  • 6

1 Answers1

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;
    }
}
IQbrod
  • 2,060
  • 1
  • 6
  • 28
  • I have tried that but it gives me an error "Generic Array creation" and does not let me perform that – kevin chua Sep 21 '20 at 08:30
  • I think that post might answer : https://stackoverflow.com/questions/3865946/error-generic-array-creation – IQbrod Sep 21 '20 at 08:31
  • The post tells me to Cast an Object into ObjectContainer, which results in my original error listed before – kevin chua Sep 21 '20 at 08:33
  • You should provide ObjectContainer class (or library providing that class) – IQbrod Sep 21 '20 at 08:33
  • 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 – IQbrod Sep 21 '20 at 08:37
  • I have added object container class within the post, ObjectContainer is a class within another class. – kevin chua Sep 21 '20 at 08:38
  • Where do you try to create such Array ? Using a simple implementation does work for me without any compilation error. I uploaded my own version with your class and it does compile – IQbrod Sep 21 '20 at 08:40
  • public class LinkedMultiHashSet implements MultiSet, Iterable { private ObjectContainer hashTable[]; public LinkedMultiHashSet(int initialCapacity) { hashTable = (ObjectContainer[]) new ObjectContainer[initialCapacity]; } } – kevin chua Sep 21 '20 at 08:42
  • Provide that information in your post, with implementation and details – IQbrod Sep 21 '20 at 08:42
  • Can you provided your error and stacktrace, it does compile for me : check EDIT 2 – IQbrod Sep 21 '20 at 08:47
  • Also do you import your custom class, your code might use one from library which is generic, your class isn't generic – IQbrod Sep 21 '20 at 08:50
  • It has been solved sorry, i created the class within the HashSet Class which was fixed once it was created outside of it – kevin chua Sep 21 '20 at 09:00