1

So I'm working with hashing and want to create an array of LinkedList. How do you add the new Object to the LinkedList using the table[index]. This is what I have so far, but when I try to call the LinkedList method add it doesn't work. Is it because everything is set to null beforehand? Do I need to add everything manually?

private void populateLinkedList(LinkedList<String>[] table, ArrayList<String> dictionary){
    for(String s:dictionary){
        String temp=findHash(s);
        System.out.print(temp + ": ");
        int hashKey=hashFunction(temp);
        Anagram obj=new Anagram(s, temp, hashKey);
                    table[hashKey].add(obj);
    }       
}

populateLinkedList(hashTable, dictionaryList); This is how I call the function.

6
  • 1
    "Is it because everything is set to null beforehand? Do I need to add everything manually?" ...probably. Commented Nov 27, 2013 at 19:15
  • 1
    ...when I try to call the LinkedList method add it doesn't work. Do you get an exception of some type or what does it do to "not work?" Commented Nov 27, 2013 at 19:19
  • What is the type of hashTable? There is no way to instantiate a LinkedList<String>[] so the method cannot be being passed the right parameter. Commented Nov 27, 2013 at 19:20
  • Are you receiving an ArrayStoreException? Commented Nov 27, 2013 at 19:39
  • @MattBall Your comment makes so much sense the OP is declaring LinkedList<String>[] arr = null; outside of the method. Commented Nov 27, 2013 at 19:40

2 Answers 2

2

An array cannot have a component type that is a parametrized type, or I should say its not useful. Basically due to type erasure, the type of the array is not known, which causes the array store check to fail.

LinkedList<String>[] table is causing your issue. The argument passed into the method cannot be of type LinkedList<String>[] because its impossible to instantiate such a type in Java.

Try the following line in your IDE, which won't compile:

LinkedList<String>[] list = new LinkedList<String>[];

Try using a:

List<LinkedList<String>> instead of LinkedList<String>[]

See the Generic Faq

Working Example

I had to stub a bunch of methods, but here is a working example:

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class ArrayStoreCheck {

    public static void main(String[] args) {
        List<LinkedList<Anagram>> lists = new ArrayList<LinkedList<Anagram>>();
        LinkedList<Anagram> anagrams = new LinkedList<Anagram>();
        lists.add(anagrams);

        List<String> dictionary = new ArrayList<String>();
        dictionary.add("one");
        dictionary.add("two");

        populateLinkedList(lists, dictionary);

        System.out.println(lists.get(0).get(0));
    }

    private static void populateLinkedList(List<LinkedList<Anagram>> table, List<String> dictionary){
        for(String s:dictionary){
            String temp=findHash(s);
            int hashKey=hashFunction(temp);
            Anagram obj=new Anagram(s, temp, hashKey);
            table.get(hashKey).add(obj);
        }       
    }

    //Stub
    private static String findHash(String s){
        return "";
    }

    //Stub
    private static int hashFunction(String s){
        return 0;
    }

    //Stub
    public static class Anagram{

        private String s;

        public Anagram(String s, String t, int key){
            this.s = s;
        }

        @Override
        public String toString() {
            return s;
        }       
    }
}
Sign up to request clarification or add additional context in comments.

13 Comments

This compiles and runs fine for me: LinkedList<String> strings = new LinkedList<String>(); LinkedList<String>[] lists = new LinkedList[20]; lists[0] = strings; strings.add("Hello"); System.out.println(lists[0].get(0));
Notice that you did not provide a type argument for the LinkedList, LinkedList<String>[] lists = new LinkedList[20];
His method doesn't compile but because he's attempting to add an Anagram to the list instead of a String.
@MadConan Here is the problem. I can insert any LinkedList into that array because the type information is lost at runtime. See: gist.github.com/kmb385/7681956
My hunch is that the argument supplied for the parameter LinkedList<String>[] table points to a null reference.
|
0

You really can't do

 int hashKey=hashFunction(temp);
 .... 
 table[hashKey].add(obj);

assuming the hashKey value is something that can be MIN to MAX integer value. You can only access elements of an array with a value that is 0 to array.length. What exception are you getting?

But I think the main problem is that you are attempting to add an Anagram type to the LinkedList which is expecting a String.

You definitely need to make sure there is an instance of LinkedList at any index you are attempting to access.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.