Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to create an array of linked lists in java

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading