为什么数组被初始化为默认值而不是java中的arraylist?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21444973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
why arrays are initialized to default values but not arraylist in java?
提问by brain storm
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
。但是,Arrays
被初始化为默认值(0 or 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;
采纳答案by tbodt
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
or null
。
On the other hand, ArrayList
s 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.
另一方面,ArrayList
s 被设计为能够调整大小。因此,您不必在创建它们时指定大小。如果起始大小大于零,则必须初始化所有这些元素,并且更容易不初始化。所以起始大小为零。
回答by Greg Hewgill
When constructing an array, the number is the actualsize 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 sizeis zero. The size of an ArrayList
can change after it is constructed.
构造数组时,数字是数组的实际大小(以后不能更改)。ArrayList
构造函数中的数字是初始容量(为元素保留的空间)但大小为零。一个ArrayList
可以在构建后的大小改变。
回答by PNS
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
收集的目的是几乎和数组一样快,没有任何空间浪费(= 预分配)。因此,它被初始化为size 0
(即,空),而不是具有多个null
值(尽管实际上底层数组被构造为指定的容量)。
Having said that, note also that an ArrayList
will store objects and not primitives (e.g., String
objects and not char
or int
primitives).
话虽如此,还要注意 an ArrayList
will 存储对象而不是原语(例如,String
对象而不是char
或int
原语)。
回答by Happy
I suppose you are looking for Collections.nCopies
:
我想你正在寻找Collections.nCopies
:
List<Integer> list = new ArrayList<Integer>(Collections.nCopies(10, 0));
回答by Edwin Dalorzo
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 个空元素的列表。
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 的列表;
回答by user207421
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 或任何其他值。
回答by Evgeniy Dorofeev
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)
允许创建具有预定义大小的列表并使用任何值对其进行初始化。顺便说一句,它在内部使用CopiesList
具有CopiesList(int n, E e)
构造函数的类
回答by Ashot Karakhanyan
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
是原始的和之间差别Object
的类型。它们的默认值是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
即使您完全初始化它也显示他的大小。因为集合size()
具有逻辑意义,而不是物理意义。
List<Integer> list = new ArrayList<Integer>(10);
Also you can fill new Collection (List) with the following static method
您也可以使用以下静态方法填充新集合(列表)
Collections.fill(list, 0) // int 0 is auto-boxed to `Integers` 0