4

下面定义的两种数据结构有什么区别?

第二个是 ArrayList,其元素的类型为“String”。但是第一个数据结构是什么?

初始化也会有所不同。谁能在这里举个例子?

    ArrayList<String>[] temp1;
    ArrayList<String> temp2;
4

6 回答 6

3

ArrayList<String>[] temp1;: This is an Array of ArrayList's that are containing Strings

ArrayList<String> temp2;: This is an ArrayList containing Strings

If you want an ArrayList of Arrays of Strings, you would have to do a ArrayList<String[]> temp3;. Note the position of the different brackets.

To initialize:

// create an array with 10 uninitialized ArrayList<String>
ArrayList<String>[] temp1 = new ArrayList[10];
// create empty lists that can be filled
for (int i=0; i<temp1.length; i++)
  temp1[i] = new ArrayList<String>();

// create an empty list of Strings
ArrayList<String> temp2 = new ArrayList<String>();

// create an empty list of String arrays
ArrayList<String[]> temp3 = new ArrayList<String[]>();
于 2012-05-15T10:17:15.870 回答
1

I provide some example to differentiate the Array of ArrayList and ArrayList of String

public class ArrayOfArrayList {

    public static void main(String[] args) {
        // Declare the Array of ArrayList
        List<String>[] arrayOfList = new ArrayList[2];

        // Declare the Object of ArrayList
        for(int i = 0; i < arrayOfList.length; i++) {
            arrayOfList[i] = new ArrayList<>();
            arrayOfList[i].add("" + (i + 1));
            arrayOfList[i].add("" + (i + 2));
        }

        // Print out the result
        for(List<String> list : arrayOfList) {
            for(String str : list) {
                System.out.println(str);
            }
        }

        // Declare the Object of ArrayList
        List<String> arrayList = new ArrayList<>();
        arrayList.add("1");
        arrayList.add("2");

        // Print out the result
        for(String str : arrayList) {
            System.out.println(str);
        }
    }
}
于 2012-05-15T10:40:00.767 回答
0

第二个是 ArrayList,其元素的类型为“String”。但是第一个数据结构是什么?

从表面上看,它似乎是一个列表数组(包含字符串)。但是数组和泛型不能很好地结合在一起。来自文章:

数组是协变但泛型不是这一事实的另一个结果是,您不能实例化泛型类型的数组(new List<String>[3] 是非法的),除非类型参数是无界通配符(new List<? >[3] 是合法的)。让我们看看如果你被允许声明泛型类型的数组会发生什么:

List<String>[] lsa = new List<String>[10]; // illegal
Object[] oa = lsa;  // OK because List<String> is a subtype of Object
List<Integer> li = new ArrayList<Integer>();
li.add(new Integer(3));
oa[0] = li; 
String s = lsa[0].get(0); 

最后一行将抛出 ClassCastException,因为您已设法将 List<Integer> 塞入应该是 List<String> 的内容中。因为数组协变允许您破坏泛型的类型安全,所以禁止实例化泛型类型的数组(类型参数为无界通配符的类型除外,如 List<?>)。

于 2012-05-15T10:05:58.993 回答
0

第一个数据结构是一个包含字符串对象的 ArrayLists 数组

于 2012-05-15T10:06:45.770 回答
0

第一个是类型的类数组ArrayList<String>。第二个只是一个ArrayList<String>(ArrayList of Strings.)

在初始化方面:

ArrayList<String>[] lists = (ArrayList<String>[])new ArrayList[10];
ArrayList<String> temp2 = new ArrayList<String>();

The first initialisation has to specify a size for the array (note this is not a size for the ArrayList) and this is where the 10 comes from in my example. It can be any size you choose of course, 10 is just an arbitrary example. It will also generate a warning, but, if you really want an array of ArrayList<String> this is AFAIK the only way for now (the reason stems from the fact generics in Java aren't reified, but array types are.)

于 2012-05-15T10:07:15.860 回答
0

Yes, first is the Array of ArrayList and will have strings value in it. second statement is only array list of Strings value.

于 2012-07-10T10:03:48.737 回答