English 中文(简体)
Java Generics: 含有通用名称的射线[复制]
原标题:Java Generics: Array containing generics [duplicate]
This question already has answers here:
Closed 11 years ago.

Possible Duplicate:
Java how to: Generic Array creation
Error generic array creation

I have been tasked with writing a Hash Table in Java, which must work with any data type. The rules on the code I am writing are as follows: - The hash table must have an array as the underlying data structures, of a size determined at the time the object is constructed - When a collision occurs, the element that collides should be placed into a linked list, which holds all of the elements at that index (key) in the hash table

因此,就基本数据类型而言,我已经做了一系列类型的联系语言(海关,而不是Java AP LinkedList)。

private LinkedList<T>[] table;

The issue is, of course, instantiating this array. Here are some of my attempts:

public HashTable(int size) {
  table = new LinkedList<T>[size];
}

这留下了汇编时通用阵列生成错误。

public HashTable(int size) {
  table = (LinkedList<T>[])(new Object[size]);
}

That causes a ClassCastException error at runtime (java.lang.Object cannot be cast to LinkedList).

The person heading the project is also unsure of how to deal with this issue. Is there any way I can change my code so that the hash table still has an array as its underlying data structure with the collisions being placed in a LinkedList?

最佳回答

这为我工作:

public class HashTable<T> {

    private LinkedList<T> table[];

    @SuppressWarnings("unchecked")
    public HashTable(int size) {
        table = new LinkedList[size];
    }

}

例如:

HashTable<String> t = new HashTable<String>(10);
t.table[0] = new LinkedList<String>();
t.table[0].add("test");
System.out.println(t.table[0].get(0));

是的,建筑商产生了警告(这解释了“不受控制”的注解),但此后,该法规没有发出更多的警告。

问题回答

Just use Object[ ] as You data Store, and edited to the specific category. 这一点在建设基础设施方面是可以接受的,在这些基础设施中,类型关系比往常难。

对于其价值而言,这是在 Java建立通用阵列的方法:

@SafeVarargs
static <E> E[] newArray(int length, E... array)
{
    return Arrays.copyOf(array, length);
}

//used in your example

    private LinkedList<T>[] table;

    public HashTable(int size) {
        table = newArray(size);
    }

这是一种理想,但你可以做这样的事情:

import java.util.LinkedList;

public class Test
{
    static class HashTable<T>
    {
        public HashTable(int size)
        {
            LinkedList<T>[] table = (LinkedList<T>[])java.lang.reflect.Array.newInstance(LinkedList.class, size);
        }
    }

    public static void main(String[] args)
    {
        HashTable<Integer> table = new HashTable<Integer>(23);
    }
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签