-2

So this is an exercice given by my teacher. We're creating a SkipList, aka a "LinkedList" with shortcuts. He gave us a class diagram with one interface (AbstractLink) and three class (StartLink and EndLink implement AbstractLink while Link extends StartLink). He also gave us a huge part of the code but i find it surprising that the interface is empty, i dont really understand why it is and he doesn't look like we need to fill it. Anyhow, in the StartLink, he told us to write the constructor.

So i tried to initialize nexts, which is an array of AbstractLink (???), with the parameter n. As far as I searched, it's impossible to do it because the type of the AbstractLink is not defined. But it leaves me clueless with what i have to change ...

public interface AbstractLink<T extends Comparable<T>> extends Comparable<AbstractLink<T>> {

}
public class StartLink<T extends Comparable<T>> implements AbstractLink<T> {

    final AbstractLink<T>[] nexts;

    public StartLink(int n){
        this.nexts = new AbstractLink<T>[n];    //my line
    }

And Eclipse gives me this : Cannot create a generic array of ChainonAbstrait

Thanks in advance

Raged
  • 13
  • 3

1 Answers1

0

Try creating an Object array and casting it to a generic type.

public class StartLink<T extends Comparable<T>> implements AbstractLink<T> {

    final AbstractLink<T>[] nexts;

    public StartLink(int n){
        this.nexts = (AbstractLink<T>[]) new Object[n];
    }
}
Henrik
  • 400
  • 1
  • 10
  • Eclipse gives me this : The type Object is not generic; it cannot be parameterized with arguments But " new Object[n] " looks ok to Eclipse Anyway, thank you for being so damn fast ! – Raged Jun 12 '19 at 16:38
  • My bad, forgot to remove the generic type when copy pasting your code. Try the edited version instead. – Henrik Jun 12 '19 at 16:40