简体   繁体   English

将对象添加到LinkedList Java数组

[英]Adding object to array of LinkedList java

So I'm working with hashing and want to create an array of LinkedList. 所以我正在使用哈希,并希望创建一个LinkedList数组。 How do you add the new Object to the LinkedList using the table[index]. 如何使用表[索引]将新对象添加到LinkedList。 This is what I have so far, but when I try to call the LinkedList method add it doesn't work. 到目前为止,这是我所拥有的,但是当我尝试调用LinkedList方法时,添加它不起作用。 Is it because everything is set to null beforehand? 是因为所有内容都预先设置为null吗? 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. LinkedList<String>[] table引起了您的问题。 The argument passed into the method cannot be of type LinkedList<String>[] because its impossible to instantiate such a type in Java. 传递给方法的参数不能为LinkedList<String>[]类型,因为无法在Java中实例化此类。

Try the following line in your IDE, which won't compile: 在您的IDE中尝试以下行,该行不会编译:

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

Try using a: 尝试使用:

List<LinkedList<String>> instead of LinkedList<String>[] List<LinkedList<String>>而不是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. 假设hashKey值可以是MIN到MAX整数值。 You can only access elements of an array with a value that is 0 to array.length . 您只能访问值为0至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 . 但是我认为主要的问题是您试图将Anagram类型添加到需要String的LinkedList中。

You definitely need to make sure there is an instance of LinkedList at any index you are attempting to access. 您绝对需要确保您尝试访问的任何索引处都有一个LinkedList实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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