为什么数组初始化为默认值但不是java中的arraylist?

时间:2022-09-25 14:08:30

The implementation of ArrayList uses Array under the hood. However, Arrays are intialized to default values (0 or null) but ArrayList are just empty. why is this?

ArrayList的实现使用了引擎盖下的Array。但是,数组初始化为默认值(0或null),但ArrayList只是空的。为什么是这样?

       int[] arr = new int[10];
       String[] arr1 = new String[11];
       System.out.println(Arrays.toString(arr));
       System.out.println(Arrays.toString(arr1));
      List<Integer> list = new ArrayList<Integer>(10);
      System.out.println(list);

      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
      [null, null, null, null, null, null, null, null, null, null, null]
      []

This means every time I use, ArrayList, I need to fill stuff in; I was trying the below part in my code and it was throwing NoSuchElementException and then I realized that it is not defaulted, where as Arrays do

这意味着每次我使用ArrayList时,我都需要填写内容;我在我的代码中尝试下面的部分并且它抛出了NoSuchElementException然后我意识到它没有默认,在Arrays中

if (list.get(i)==null){
         list.add(i,x);
  else:
        list.add(i,list.get(i)+x)

EDIT:

even List<Integer> list = new ArrayList<Integer>(10);
prints [] although I initialized the size;

8 个解决方案

#1


4  

When you create an array, you specify the size. This is required because the size of arrays can't be changed after they are created. Something must go in each element of the array, then, and the most obvious thing to put is 0 or null.

创建数组时,指定大小。这是必需的,因为创建数组后无法更改数组的大小。那么必须在数组的每个元素中都有东西,最明显的东西是0或null。

On the other hand, ArrayLists are designed to be able to be resized. So you shouldn't have to specify the size when you create them. If the starting size is more then zero, it would have to initialize all those elements, and it's easier not to. So the starting size is zero.

另一方面,ArrayLists旨在能够调整大小。因此,您不必在创建它们时指定大小。如果起始大小大于零,则必须初始化所有这些元素,并且更容易不这样做。所以起始大小为零。

#2


12  

When constructing an array, the number is the actual size of the array (and you can't change it later). The number in the ArrayList constructor is the initial capacity (space reserved for elements) but the size is zero. The size of an ArrayList can change after it is constructed.

构造数组时,数字是数组的实际大小(以后不能更改)。 ArrayList构造函数中的数字是初始容量(为元素保留的空间),但大小为零。 ArrayList的大小在构造之后可以改变。

#3


4  

The reason of null and 0 is difference between primitive and Object types. Their default values are null and 0(only false for boolean) correspondingly. See The Java Tutorial

null和0的原因是原始类型和对象类型之间的差异。它们的默认值相应地为null和0(仅对布尔值为false)。请参阅Java教程

ArrayList is not physically empty after creating. It has initial capacity, but it shows his size 0 even you initialized it exactly.Because Collection size() has logical meaning, not phisical.

创建后,ArrayList不是物理空的。它具有初始容量,但它显示了他的大小0,即使你准确地初始化它。因为Collection size()具有逻辑意义,而不是物理。

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

Also you can fill new Collection (List) with the following static method

您还可以使用以下静态方法填充新的Collection(List)

Collections.fill(list, 0) // int 0 is auto-boxed to `Integers` 0 

#4


3  

You could do it like this:

你可以这样做:

List<Integer> items = Arrays.asList(new Integer[10]);

And you would get a list with 10 null elements.

你会得到一个包含10个null元素的列表。

Similarly,

Integer[] numbers = new Integer[10];
Arrays.fill(numbers,0);
List<Integer> list = Arrays.asList(numbers);

And you get a list with 10 elements initialised to 0;

你得到一个列表,其中10个元素初始化为0;

#5


3  

As an ArrayList doesn't initially contain any elements, it can't default the value of those elements to null or any other value.

由于ArrayList最初不包含任何元素,因此不能将这些元素的值默认为null或任何其他值。

#6


2  

The purpose of the ArrayList collection is to be virtually as fast as an array, without any waste (= preallocation) of space. Therefore, it is initialized to size 0 (i.e., empty), as opposed to having multiple null values (although practically speaking the underlying array is constructed to the specified capacity).

ArrayList集合的目的几乎与数组一样快,没有任何浪费(=预分配)空间。因此,它被初始化为大小0(即,空),而不是具有多个空值(尽管实际上,底层阵列被构造为指定的容量)。

Having said that, note also that an ArrayList will store objects and not primitives (e.g., String objects and not char or int primitives).

话虽如此,还要注意ArrayList将存储对象而不是基元(例如,String对象而不是char或int基元)。

#7


2  

I suppose you are looking for Collections.nCopies:

我想你正在寻找Collections.nCopies:

List<Integer> list = new ArrayList<Integer>(Collections.nCopies(10, 0));

#8


1  

This is because there is no constructor in ArrayList which could create an instance of certain size. Collections.nCopies(int n, T o) allows to create a List with predefined size and initialize it with any values. BTW internally it uses CopiesList class which has CopiesList(int n, E e) constructor

这是因为ArrayList中没有构造函数可以创建特定大小的实例。 Collections.nCopies(int n,T o)允许创建具有预定义大小的List,并使用任何值对其进行初始化。 BTW内部使用CopiesList类,它具有CopiesList(int n,E e)构造函数

#1


4  

When you create an array, you specify the size. This is required because the size of arrays can't be changed after they are created. Something must go in each element of the array, then, and the most obvious thing to put is 0 or null.

创建数组时,指定大小。这是必需的,因为创建数组后无法更改数组的大小。那么必须在数组的每个元素中都有东西,最明显的东西是0或null。

On the other hand, ArrayLists are designed to be able to be resized. So you shouldn't have to specify the size when you create them. If the starting size is more then zero, it would have to initialize all those elements, and it's easier not to. So the starting size is zero.

另一方面,ArrayLists旨在能够调整大小。因此,您不必在创建它们时指定大小。如果起始大小大于零,则必须初始化所有这些元素,并且更容易不这样做。所以起始大小为零。

#2


12  

When constructing an array, the number is the actual size of the array (and you can't change it later). The number in the ArrayList constructor is the initial capacity (space reserved for elements) but the size is zero. The size of an ArrayList can change after it is constructed.

构造数组时,数字是数组的实际大小(以后不能更改)。 ArrayList构造函数中的数字是初始容量(为元素保留的空间),但大小为零。 ArrayList的大小在构造之后可以改变。

#3


4  

The reason of null and 0 is difference between primitive and Object types. Their default values are null and 0(only false for boolean) correspondingly. See The Java Tutorial

null和0的原因是原始类型和对象类型之间的差异。它们的默认值相应地为null和0(仅对布尔值为false)。请参阅Java教程

ArrayList is not physically empty after creating. It has initial capacity, but it shows his size 0 even you initialized it exactly.Because Collection size() has logical meaning, not phisical.

创建后,ArrayList不是物理空的。它具有初始容量,但它显示了他的大小0,即使你准确地初始化它。因为Collection size()具有逻辑意义,而不是物理。

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

Also you can fill new Collection (List) with the following static method

您还可以使用以下静态方法填充新的Collection(List)

Collections.fill(list, 0) // int 0 is auto-boxed to `Integers` 0 

#4


3  

You could do it like this:

你可以这样做:

List<Integer> items = Arrays.asList(new Integer[10]);

And you would get a list with 10 null elements.

你会得到一个包含10个null元素的列表。

Similarly,

Integer[] numbers = new Integer[10];
Arrays.fill(numbers,0);
List<Integer> list = Arrays.asList(numbers);

And you get a list with 10 elements initialised to 0;

你得到一个列表,其中10个元素初始化为0;

#5


3  

As an ArrayList doesn't initially contain any elements, it can't default the value of those elements to null or any other value.

由于ArrayList最初不包含任何元素,因此不能将这些元素的值默认为null或任何其他值。

#6


2  

The purpose of the ArrayList collection is to be virtually as fast as an array, without any waste (= preallocation) of space. Therefore, it is initialized to size 0 (i.e., empty), as opposed to having multiple null values (although practically speaking the underlying array is constructed to the specified capacity).

ArrayList集合的目的几乎与数组一样快,没有任何浪费(=预分配)空间。因此,它被初始化为大小0(即,空),而不是具有多个空值(尽管实际上,底层阵列被构造为指定的容量)。

Having said that, note also that an ArrayList will store objects and not primitives (e.g., String objects and not char or int primitives).

话虽如此,还要注意ArrayList将存储对象而不是基元(例如,String对象而不是char或int基元)。

#7


2  

I suppose you are looking for Collections.nCopies:

我想你正在寻找Collections.nCopies:

List<Integer> list = new ArrayList<Integer>(Collections.nCopies(10, 0));

#8


1  

This is because there is no constructor in ArrayList which could create an instance of certain size. Collections.nCopies(int n, T o) allows to create a List with predefined size and initialize it with any values. BTW internally it uses CopiesList class which has CopiesList(int n, E e) constructor

这是因为ArrayList中没有构造函数可以创建特定大小的实例。 Collections.nCopies(int n,T o)允许创建具有预定义大小的List,并使用任何值对其进行初始化。 BTW内部使用CopiesList类,它具有CopiesList(int n,E e)构造函数