java 如何将arrayList大小设置为null?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2557692/
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
How to set arrayList size as null?
提问by Jessy
String a []= {null,null,null,null,null};
//add array to arraylist
ArrayList<Object> choice = new ArrayList<Object>(Arrays.asList(a));
System.out.println(choice.size());
Why the size of the arrayList choiceis 5 when all the elements have been set to null
choice当所有元素都设置为 5 时,为什么 arrayList 的大小是 5null
采纳答案by Frederik Wordenskjold
Because the arraylist still has 5 elements in it. They might be null, but they are still present in the list.
因为 arraylist 中仍然有 5 个元素。它们可能为空,但它们仍然存在于列表中。
You can empty the ArrayList, by calling choice.clear();
您可以通过调用清空 ArrayList choice.clear();
回答by Uri
I think that what is confusing you is that you are thinking of the ArrayList as an ArrayList of objects, not as an ArrayList of references to objects.
我认为让您感到困惑的是,您将 ArrayList 视为对象的 ArrayList,而不是对象引用的 ArrayList。
An ArrayList of Object represents an index-accessible list of referencesto objects.
Object 的 ArrayList 表示对象引用的索引可访问列表。
It is valid for such a reference to not refer to a real object but instead be "null".
这种引用不引用真实对象而是“空”是有效的。
Hence, you have five reference "slots", each with a value of null.
因此,您有五个引用“槽”,每个槽的值为空。
This is not the same as a series of four nulls, or zero nulls.
这与一系列四个空值或零空值不同。
Look at your initial and primitive array of strings - its length is five, not zero.
看看你的初始和原始字符串数组 - 它的长度是五,而不是零。
Or even simpler, when you have a class with a field of type Object, it still takes space, regardless of whether it is actually referring to something or is null. Otherwise, you wouldn't be able to instantiate the class and not need to reallocate it when you actually assigned something to the field.
或者更简单的是,当你有一个字段类型为 Object 的类时,它仍然需要空间,不管它是实际引用某物还是为空。否则,您将无法实例化该类,并且在您实际将某些内容分配给该字段时不需要重新分配它。
回答by Justin Ardini
From the Javadocs for ArrayList:
来自 ArrayList 的 Javadocs:
Resizable-array implementation of the List interface. Implements all optional list operations, and permits all elements, including null.
List 接口的可调整大小的数组实现。实现所有可选的列表操作,并允许所有元素,包括 null。
ArrayLists can hold null elements, so the size is five. Keep in mind that if you simply declare a new ArrayList(5), it will have a initial capacity of 5, but the size()will be 0.
ArrayLists 可以容纳空元素,因此大小为 5。请记住,如果您简单地声明 a new ArrayList(5),它的初始容量size()将为5,但将为 0。
回答by gurbieta
This is because the ArrayList is not null, it holds five objects (all are null), anyway an ArrayList never will have a null size, at least the size is 0 (zero), and if the ArrayList is not initialized (is null) and you try to access to the function size() it will threw a NullPointerException. I hope this information will be useful for you, and sorry for my bad english :(
这是因为ArrayList不为空,它持有五个对象(都是空的),反正一个ArrayList永远不会有空大小,至少大小为0(零),如果ArrayList没有初始化(为空)并且您尝试访问函数 size() 它将抛出 NullPointerException。我希望这些信息对您有用,并为我的英语不好:(

