-1

Hello StackOverflow community , I am trying to create the following diagram in java : enter image description here

There is Cellule Class ( cell which contains : an unknown type attribut named val , an integer named val , and a pointer to another Cell named next ) , and the DictionnaireHacahge Class which declares a pointer to an array of Cellule named dictH which I am trying to instanciate further in the **constructor **. PS: the two class are in the same package inf353 . but when I am trying to code it with java I am facing a problem in the constructor : the constructor the error : enter image description here

and here is the code : Cellule.java :

package inf353;

public class Cellule<T>{
        public T val;
        public int index ;
        public Cellule next;

        public Cellule() {
            super();
        }

DictionnaireHachage class :

package inf353;

public class DictionnaireHachage implements Dictionnaire{
    Cellule<String>[] dictH ;
    int index = 0;
    public void DictionnaireHachage(){
        dictH = new Cellule<String>[100];
        index=0;
    }
  • What the error mentionnned by the red underlining ? – azro Nov 27 '22 at 08:10
  • - I added the error screenshot - I tried to read the https://stackoverflow.com/questions/20580454/generic-array-creation-with-diamond-operator twice but I don't think it's the same problem or I can't see it , if you could explain it I would appreciate it – Oukkal Yacine Nov 27 '22 at 08:20
  • @OukkalYacine this is a better dupe, but I couldn't find it before: https://stackoverflow.com/questions/7810074/array-of-generic-list. And even spot-on: https://stackoverflow.com/questions/217065/cannot-create-an-array-of-linkedlists-in-java (it even asks about linked lists) – knittl Nov 27 '22 at 08:34

1 Answers1

2

It is not possible to create an array with diamond:

Cellule<String>[] arr = new Cellule<String>[10];

But the good news. We all know (I hope), that diamond operations are live only at compile time. At runtime there're always Object. So you can create the array not using diamond:

Cellule<String>[] arr = new Cellule[10];

P.S. It is better to use List<Cellule<String>> instead

Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35