简体   繁体   English

添加通用类型的元素 <T> 到LinkedList的arrayList

[英]Adding a element of Generic type <T> to an arrayList of LinkedList's

I want to make a hash table class in java where I store the key,value pairs in an ArrayList of Linked List's. 我想在Java中创建一个哈希表类,在其中将键值对存储在链表的ArrayList中。 I do this by declaring 我通过声明来做到这一点

 ArrayList<LinkedList<T>> storage = new ArrayList();

I then want to create a linkList object that I can use to then create a new linked list inside of each index of the arrayList. 然后,我想创建一个linkList对象,该对象可用于在arrayList的每个索引内创建一个新的链表。 I do this by declaring: 我通过声明:

  LinkedList<T> list = new LinkedList<T>();

Then I have my add function set up to add elements to the first index of the LinkedList that is inside the Hashed key index of the arrayList as such: 然后设置我的add函数,将元素添加到arrayed的Hashed键索引内的LinkedList的第一个索引中,如下所示:

public void add(K key, T value){    
int arrayListIndex = (key.hashCode()) % this.initialCapacity;
    System.out.println(arrayListIndex); //This tells us where we access the Array List;


    if (hashBrown.get(arrayListIndex) == null){

        hashBrown.add(arrayListIndex, list);
        hashBrown.get(arrayListIndex).addFirst(value);
    }
}

Everytime I run this code I receive an error where my index is 7 and my size is 0. This causes an error: 每次运行此代码时,我都会收到一个错误,索引为7,大小为0。这将导致错误:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 7, Size: 0
    at java.util.ArrayList.rangeCheck(ArrayList.java:571)
    at java.util.ArrayList.get(ArrayList.java:349)
    at FastHashtable.add(FastHashtable.java:72)
    at FastHashtable.main(FastHashtable.java:145)

I am unable to track down where this index out of bounds error is coming from can anyone offer advice. 我无法找到该索引超出范围错误的出处,任何人都可以提供建议。 I am fairly new at working with ArrayLists which makes me think my original declaration of the arrayList is incorrect. 我在使用ArrayLists时是相当陌生的,这使我认为arrayList的原始声明不正确。

You are confusing an ArrayList s capacity with its size . 您正在将ArrayList容量与其大小混淆。 From the Oracle Java documentation: 从Oracle Java文档中:

Each ArrayList instance has a capacity . 每个ArrayList实例都有一个容量 The capacity is the size of the array used to store the elements in the list. 容量是用于在列表中存储元素的数组的大小。 It is always at least as large as the list size. 它总是至少与列表大小一样大。 As elements are added to an ArrayList , its capacity grows automatically. 将元素添加到ArrayList ,其容量会自动增长。 The details of the growth policy are not specified beyond the fact that adding an element has constant amortized time cost. 除了添加元素具有固定的摊销时间成本外,没有指定增长策略的详细信息。

Instead, you should be looking at creating a plain array (eg Object[] a = new Object[maxSize] ) in which you can actually assign objects (linked lists in this case) at arbitrary index values. 相反,您应该考虑创建一个普通数组(例如Object[] a = new Object[maxSize] ),在其中您可以实际为任意索引值分配对象(在这种情况下为链接列表)。 If you only want to store linked lists, create a LinkedList<T>[] array. 如果只想存储链接列表,请创建一个LinkedList<T>[]数组。

You cannot add to a list at index 7 if there are 0 elements in it. 如果其中有0个元素,则不能将其添加到索引7的列表中。 You can only add at the end (with the add method without index) or at a position that is no larger than the current list's size() . 您只能在末尾添加(使用不带索引的add方法)或在不大于当前列表的size()

When the list is empty and you add something at index 7, what do you expect the list to contain at the first position then, and what at index 6? 当列表为空并且在索引7处添加内容时,您期望列表在第一位置包含什么,在索引6处包含什么? (I once created a subclass of list to fill everything up to the index with null when the addition index is larger than the list size, but this behaviour is not universal enough to be part of List 's semantics. (我曾经创建过一个list的子类,当附加索引大于列表的大小时,它会使用null填充索引中的所有内容,但是这种行为并不通用,不足以成为List语义的一部分。


[edit in response to comment here and to praseodym's answer] [编辑以回应此处的评论和praseodym的回答]

You could simply fill the (array) list with nulls replace those with a linked list when the respective position is first accessed. 您可以简单地用空值填充(数组)列表,而在首次访问相应位置时,将其替换为链接列表。 (Be sure to use set and not add , though, which is probably what you want above as well.) Alternatively, you could create an array of the desired size (that will be full of null s by default) and "wrap" it into a (non-resizable) list via Arrays.asList . (不过,请确保使用set而不是add ,这可能也是您上面想要的。)或者,您可以创建所需大小的数组(默认情况下将充满null )并将其“包装”通过Arrays.asList放入一个(不可调整大小的)列表。 You have to ignore an "unchecked conversion" warning then, however (not that you could avoid warnings when using an array). 但是,您必须忽略“未经检查的转换”警告(但是,并不是在使用数组时可以避免出现警告)。 Also, I suggest you read " Program to an interface, not an implementation ". 另外,我建议您阅读“ 编程到接口,而不是实现 ”。

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

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