在Java中通用阵列 [英] Generic arrays in Java

查看:92
本文介绍了在Java中通用阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OK,我已经google'ing网络,我似乎无法找到任何解决我的问题。我发现很多的解决方案,只是没有任何适合。

OK, I've been google'ing the web, and I just can't seem to find any solution to my problem. I found lots of solutions, just not any that fit.

我需要创建泛型的数组。但是,泛型类型本身延伸相媲美。当我尝试以下方法:

I need to create an array of generics. But the generic type itself extends Comparable. When I try the following:

public class Hash<T extends Comparable<String>> {
    private T[] hashTable;
    private int tableSize;

    Hash(int records, double load) {
        tableSize = (int)(records / loadFactor);
        tableSize = findNextPrime(tableSize);
        hashTable = (T[])(new Object[tableSize]);  //Error: Ljava.lang.Object; cannot be cast to [Ljava.lang.Comparable;
    }
}

问题是,对象不能被转换为扩展可比一般的。有没有办法解决?

The problem is that the Object cannot be cast as a generic that extends Comparable. Is there a way around this?

推荐答案

泛型和数组不混合,基本上是这样。简短的回答是,你可以解决这个问题。较长的答案是,你可能不应该,我会解释为什么。

Generics and arrays don't mix, basically. The short answer is that you can work around this problem. The longer answer is that you probably shouldn't and I'll explain why.

您可以使用<一个href=\"http://download.oracle.com/javase/6/docs/api/java/lang/reflect/Array.html#newInstance%28java.lang.Class,%20int...%29\"><$c$c>Array.newInstance()像这样的:

You could use Array.newInstance() like this:

private Comparable[] hashtable;

...

hashtable = (Comparable)Array.newInstance(Comparable.class, tableSize);

但您不能创建参数化类型的数组。

but you can't create an array of your parameterized type.

数组是的的。这意味着他们保留在运行时其元素的类型。 Java的泛型不是这样的。他们使用的类型擦除的基本上掩盖了正在发生的隐式转换。理解这一点很重要。

Arrays are covariant. That means they retain the type of their elements at runtime. Java's generics are not. They use type erasure to basically mask the implicit casting that is going on. It's important to understand that.

所以,当你创建一个对象数组,你不能把它转换,比方说,一个可比阵列(或任何其他类型),因为这是不正确的。

So when you create an Object array you can't cast it to, say, a Comparable array (or any other type) because that is not correct.

给你举个例子。随着仿制药,这是完全合法的:

To give you an example. With generics this is perfectly legal:

List<String> list = new ArrayList<String>();
List<Integer> list2 = (List<Integer>)list;
list.add(3);

这也是为什么你不能做到这一点:

It's also why you can't do this:

public <T> T newInstance(T t) {
  return new T(); // error!
}

,即在运行时没有T的类的知识。这就是为什么上面code更常写为:

ie at runtime there is no knowledge of T's class. This is why the above code is more often written as:

public <T> T newInstance(T t, Class<T> clazz) {
  return clazz.newInstance();
}

由于他们是为通用参数没有运行时类型。但随着数组:

because their is no runtime type for the generic argument. But with arrays:

String arr[] = new String[10];
Integer arr2[] = (Integer[])arr; // error!

你应该在这种情况下,(恕我直言)做什么是不使用数组,但使用的ArrayList 。在所有诚实,很少有理由使用数组通过一个的ArrayList 和泛型是这只是一个例子。

What you should be doing in this case (imho) is not using arrays but using an ArrayList. In all honesty, there is very little reason to use arrays over an ArrayList and generics is just one example of that.

对于一个更好,更完整的解释见(优秀) Java泛型常见问题解答

For a better and more complete explanation see the (excellent) Java Generics FAQ:

否,因为它不是类型安全的。

Can I create an array whose component type is a concrete parameterized type?

No, because it is not type-safe.

数组是协变的,这意味着
  超引用数组是一
  亚型的阵列的超
  引用。也就是说,对象[] 是一个
  的超的String [] 和一个字符串
  阵列可通过一个被访问
  类型的引用变量对象[]

Arrays are covariant, which means that an array of supertype references is a supertype of an array of subtype references. That is, Object[] is a supertype of String[] and a string array can be accessed through a reference variable of type Object[].

...

这篇关于在Java中通用阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆