0

Issue solved, no longer needed

kevin chua
  • 13
  • 5

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
  • 1,590
  • 1
  • 2
  • 20
  • 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