数组的Java泛型语法 [英] Java Generics Syntax for arrays
问题描述
List< ArrayList> [] myArray;
我认为它应该声明一个数组,其中每个元素都是 List
(例如, LinkedList
或 ArrayList
),并要求每个 List
包含 ArrayList
objects。
我推理:
列表< String> someList; //字符串对象列表
List< ArrayList> someList; // ArrayList对象列表
List< ArrayList> [] someListArray; // ArrayList对象List的数组
运行一些测试后,我确定它接受一个数组其中每个元素都是 LinkedList
对象,并且不指定LinkedList对象包含的内容。
因此 List< ArrayList>
指定 List
必须包含的内容,但 List< ArrayList> []
指定如何实现 List
。
我错过了什么?
以下是我的测试。
import java.util.ArrayList;
import java.util.List;
import java.util.LinkedList;
public class Generics1 {
public static void main(String [] args){
List< ArrayList> [] someListArray ;
someListArray = getArrayWhereEachElementIsAnArrayListObject();
//为什么这符合声明?
// someListArray [0] => ArrayList对象持有字符串
someListArray = getArrayWhereEachElementIsAListOfArrayListObjects();
// someListArray [0] =>包含ArrayList对象的ArrayList对象
$ public static List [] getArrayWhereElementIsAnArrayListObject(){
List [] arrayOfLists = new ArrayList [2];
arrayOfLists [0] = getStringList();
arrayOfLists [1] = getIntegerList();
返回arrayOfLists;
public static List [] getArrayWhereEachElementIsAListOfArrayListObjects(){
List list1 = new ArrayList();
list1.add(getArrayList());
List list2 = new ArrayList();
list2.add(getArrayList());
List [] arrayOfListsOfArrayLists = new ArrayList [2];
arrayOfListsOfArrayLists [0] = list1;
arrayOfListsOfArrayLists [1] = list2;
返回arrayOfListsOfArrayLists;
public static List getStringList(){
List stringList = new ArrayList();
stringList.add(one);
stringList.add(two);
返回stringList;
public static List getIntegerList(){
List intList = new ArrayList();
intList.add(new Integer(1));
intList.add(new Integer(2));
return intList;
public static ArrayList getArrayList(){
ArrayList arrayList = new ArrayList();
返回arrayList;
}
}
答案是数组只能保存指定类型。并且基因化的班级没有通用化。也就是说,List< ArrayList>的运行时类型只是List。泛型在运行时被删除(谷歌的删除墙更多)。
所以这个:
$ p $ List< ArrayList> [] myArray
确实意味着:
List [] myArray
没有类型安全的方法来声明你的'重新尝试申报。一般来说,我建议你在这种情况下使用List而不是数组。有些人甚至认为,现在我们有泛型,数组应该被视为已弃用的类型。我不能说我愿意走得那么远,但是你应该考虑一个集合是否是一个更好的选择,只要你被引用到一个数组中。
这本书由Naftalin和Wadler提供的 Java泛型和集合是您可能对泛型有问题的极好参考。或者,当然,泛型常见问题是您的标准在线参考。
What data structure does the following declaration specify?
List<ArrayList>[] myArray;
I think it should declare an array where each element is a List
(e.g., a LinkedList
or an ArrayList
) and require that each List
contain ArrayList
objects.
My reasoning:
List<String> someList; // A List of String objects
List<ArrayList> someList; // A List of ArrayList objects
List<ArrayList>[] someListArray; // An array of List of ArrayList objects
After running some tests, I determined that it accepts an array where each element is an LinkedList
object and does not specify what the LinkedList objects contain.
So List<ArrayList>
specifies what the List
must contain, but List<ArrayList>[]
specifies how the List
must be implemented.
Am I missing something?
Here are my tests.
import java.util.ArrayList;
import java.util.List;
import java.util.LinkedList;
public class Generics1 {
public static void main(String[] args) {
List<ArrayList>[] someListArray;
someListArray = getArrayWhereEachElementIsAnArrayListObject();
// Why does this satisfy the declaration?
//someListArray[0] => ArrayList object holding Strings
someListArray= getArrayWhereEachElementIsAListOfArrayListObjects();
//someListArray[0] => ArrayList object holding ArrayList objects
}
public static List[] getArrayWhereEachElementIsAnArrayListObject() {
List[] arrayOfLists = new ArrayList[2];
arrayOfLists[0] = getStringList();
arrayOfLists[1] = getIntegerList();
return arrayOfLists;
}
public static List[] getArrayWhereEachElementIsAListOfArrayListObjects() {
List list1 = new ArrayList();
list1.add(getArrayList());
List list2 = new ArrayList();
list2.add(getArrayList());
List[] arrayOfListsOfArrayLists = new ArrayList[2];
arrayOfListsOfArrayLists[0] = list1;
arrayOfListsOfArrayLists[1] = list2;
return arrayOfListsOfArrayLists;
}
public static List getStringList() {
List stringList= new ArrayList();
stringList.add("one");
stringList.add("two");
return stringList;
}
public static List getIntegerList() {
List intList= new ArrayList();
intList.add(new Integer(1));
intList.add(new Integer(2));
return intList;
}
public static ArrayList getArrayList() {
ArrayList arrayList = new ArrayList() ;
return arrayList;
}
}
The answer is that arrays can only hold reified types. And generified classes are not reified. That is, the runtime "type" of the List<ArrayList> is just List. Generics are erased at runtime (google "wall of erasure" for more).
So this:
List<ArrayList>[] myArray
really means:
List[] myArray
There is no type-safe way to declare what you're trying to declare. Generally, I'd recommend you use a List instead of an array in this case. Some people have gone so far as to suggest that arrays should be treated as deprecated types now that we have generics. I can't say I'm willing to go that far but you should consider whether a collection is a better alternative whenever you're drawn to an array.
The book Java Generics and Collections by Naftalin and Wadler is an excellent reference for questions you might have about generics. Or, of course, the Generics FAQ is your canonical online reference.
这篇关于数组的Java泛型语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!