简体   繁体   English

我们可以拥有一个集合数组吗?

[英]Can we have an Array of a collection?

I was trying create an array of a collection as follows. 我正在尝试创建一个集合数组,如下所示。

ArrayList<Integer> ar[]=new ArrayList<Integer>[50];

but it gives me an error -> generic array creation can anybody explain me why is it? 但它给了我一个错误 - > 通用数组创建可以任何人解释我为什么?

You can't create arrays of generic types. 您无法创建泛型类型的数组。 Use collection of collections instead: 改为使用集合集合:

ArrayList<ArrayList<Integer>> = new ArrayList<ArrayList<Integer>>();

Why can't we create an array of generic type? 为什么我们不能创建一个泛型类型的数组? Array stores they exact type internally, but due to the type erasure at runtime there will be no generic type. 数组在内部存储它们的确切类型,但由于运行时类型擦除,因此不存在泛型类型。 So, to prevent you from been fooled by this (see example below) you can't create an array of generic type: 因此,为了防止您被这种愚弄(参见下面的示例),您无法创建泛型类型的数组:

//THIS CODE WILL NOT COMPILE
ArrayList<Integer>[] arr = new ArrayList<Integer>[5];
Object[] obj = arr;
obj[0] = new ArrayList<Long>(); //no one is safe

ArrayList is internally a 1D array itself. ArrayList本身就是一维数组。 what you need 2D array so you can create 你需要什么2D数组,所以你可以创建

ArrayList<Integer[]> ar=new ArrayList<Integer[]>();

or 要么

ArrayList<ArrayList<Integer>> = new ArrayList<ArrayList<Integer>>();

Actually you can have an Array of Collection, it just is not allowed that the Collection has a specific type. 实际上你可以拥有一个Collection of Array,但不允许Collection具有特定的类型。
You can do something like 你可以做点什么

    ArrayList<?>[] ar = new ArrayList<?>[50];  
    // or ArrayList[] ar = new ArrayList[50];
    ar[0] = new ArrayList<Integer>();

but you will not have the benefits of Generics - there is no type information for the content of the Collection, you will need to cast when reading from it 但是你没有Generics的好处 - 没有关于Collection内容的类型信息,你需要在阅读它时进行投射

    Integer i = (Integer) ar[0].get(0);

You could do something like this 你可以这样做

     ArrayList<Integer> ar[]= new ArrayList[50];

     ArrayList<Integer> intArr  = new ArrayList<Integer>();
     ArrayList<Long> longArr = new ArrayList<Long>();

     ar[0]=intArr;
     ar[1]= longArr; //  compile error Type mismatch: cannot convert from ArrayList<Long> to ArrayList<Integer>

You can have an array "technically" of type ArrayList but its a bit nit picky. 你可以拥有一个“技术上”类型为ArrayList的数组,但它有点挑剔。 Create it as an ArrayList<ArrayList<Integer>> list = ArrayList<ArrayList<Integer>(); 将其创建为ArrayList<ArrayList<Integer>> list = ArrayList<ArrayList<Integer>(); and convert it using toArray(ArrayList<Integer>[list.size()]); 并使用toArray(ArrayList<Integer>[list.size()]);转换它toArray(ArrayList<Integer>[list.size()]); .

Example: 例:

    ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>();

    list.add(new ArrayList<Integer>());
    list.add(new ArrayList<Integer>());
    list.add(new ArrayList<Integer>());

    int count = 1;

    for(ArrayList<Integer> AList: list) {
        for(int i = 0; i < 4; i++)
            AList.add(count++);
    }

    ArrayList<Integer>[] check = list.toArray(new ArrayList[list.size()]);

    for(ArrayList<Integer> AL : check) {
        for(Integer i:AL) {
            System.out.print(i + " ");
        }
        System.out.println();
    }

Output: 输出:

 1 2 3 4 
 5 6 7 8 
 9 10 11 12 

Works and is an ArrayList array Works并且是一个ArrayList数组

The answer to you question can be found in the Java Language Specification. 您可以在Java语言规范中找到问题的答案。 You are trying to create an array using Array Creation Expression . 您正在尝试使用Array Creation Expression创建数组。 It says the following: "It is a compile-time error if the ClassOrInterfaceType does not denote a reifiable type". 它说如下:“如果ClassOrInterfaceType不表示可重新生成的类型,则为编译时错误”。 Because arrays in Java are created at runtime, their type information should be completely available at runtime. 因为Java中的数组是在运行时创建的,所以它们的类型信息应该在运行时完全可用。 In other words array element type should be a reifiable type . 换句话说,数组元素类型应该是可再生类型 Generics in Java are implemented using type erasure (ie only subset of generics compile-time type information is available at runtime) hence they are not reifiable by definition and therefore you cannot create arrays of generic types. Java中的泛型是使用类型擦除实现的(即,只有泛型编译时类型信息的子集在运行时可用)因此它们不能根据定义进行重新定义,因此您无法创建泛型类型的数组。

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

相关问题 我们如何访问数组到映射器? - How we can have access to an array into a mapper? 我们可以在属性文件中将值作为值的数组吗? - Can we have value inside properties file as an array of values? 我们可以在我们自己的 JVM 中实现 C4 垃圾收集机制吗? 或者我们将不得不只使用 AZUL JVM(Zing) - Can we implement C4 garbage collection mechanism in our own JVM?Or we will have to use only AZUL JVM(Zing) 我们可以在Java中使用AtomicEnum吗? - Can we have an AtomicEnum in Java? 我们可以在doInBackground()中拥有runOnUiThread吗 - Can we have runOnUiThread in doInBackground() 为什么我们不能让 ArrayList 保存 int 类型的值,但我们可以让 Array[] 保存 int,当 ArrayList 和数组都是对象时? - Why can't we have an ArrayList hold values of type int but we can have an Array[] hold int, when both ArrayList and an array are objects? MultipleBagFetchException - 如果我们只有一个集合怎么办 - MultipleBagFetchException - what in case when we have only one collection 为什么我们在Java中使用Arrays和Array - Why we have Arrays and Array in Java 我们可以在BasicNameValuePair中发送和数组吗 - can we send and array in BasicNameValuePair 我们可以将JButton存储在数组中吗? - Can we store JButtons in an array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM