简体   繁体   中英

Adding object to array of LinkedList java

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM