Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

just asked this question and didn\'t get an answer that would work, I\'m hoping

ID: 3776672 • Letter: J

Question

just asked this question and didn't get an answer that would work, I'm hoping it was just me being unclear so let me try to be more specific.

Java Programming:

Trying to make an array of String type linked lists:

    LinkedList<String>[] hashList = new LinkedList[5];
   hashList[0].add("one");

end up with a NullPointerException.

The important part of this question is -> How does one make an array of LinkedList<String>?
Note: it must be an array of String based linked lists; any other structure willl not work
I have tried all sorts of combinations of <>,[], and String and it seems to start fine in the constructor of my hash table (the array of linked lists is shown in the debugger on NetBeans) but as soon as I try to add something I get a NullPointerException..
I'm specifically trying to eventually create a structure that would look like this if printed:

0:
1: blah->blah2
2: hello
3:
4: blank
5: super->duper->excitement

where the index of the array is created from a hash function.

Explanation / Answer

LinkedList<String>[] hashList = new LinkedList[5];
       hashList[0]=new LinkedList<>();
       hashList[0].add("one");

====================================================

Write above line hashList[0]=new LinkedList<>(); in your code.

When you create array of linkedlist you have to initialise objects using constructor.Hence Need to do initialisation of object to avoid null pointer exception.

Full code:

==================================================================

mport java.util.LinkedList;
import java.util.Scanner;

public class Driver {

   public static void main(String[] args) {
       LinkedList<String>[] hashList = new LinkedList[5];
       hashList[0]=new LinkedList<>();
       hashList[0].add("one");
       System.out.println("hashList is: " + hashList[0]);
           }

}

===========================================

Output:

hashList is [one]