带有空元素的 Java ArrayList 大小 []
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27807086/
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
Java ArrayList size with empty element []
提问by Rui Huang
I have one ArrayList, for some reason i have to add empty string element "":
我有一个 ArrayList,出于某种原因,我必须添加空字符串元素“”:
ArrayList<String> rList = new ArrayList<String>();
rList.add("");
When I read the size of the array list rList then, rList.size() == 1 and rList.isEmpty() == false.
当我读取数组列表 rList 的大小时,rList.size() == 1 和 rList.isEmpty() == false。
How can I check this kind of array list? does the empty element cause the size equals 1 and not empty?
如何检查这种数组列表?空元素是否导致大小等于 1 而不是空?
回答by Makoto
Even though you're adding an empty string to your list, you're still adding somethingto the list. And, that's what counts.
即使您向列表中添加了一个空字符串,您仍然在向列表中添加一些内容。而且,这才是最重要的。
The data may be empty (or even null
, as an ArrayList
will allow you to insert null
), but the size will increase (and the list won't be empty).
数据可能是空的(甚至null
,因为它ArrayList
允许您插入null
),但大小会增加(并且列表不会为空)。
Now, if you only want to check that there's just that sortof element in your list (as you state in comments), then that's possible like this:
现在,如果您只想检查列表中是否只有那种元素(如您在评论中所述),那么可能是这样的:
if(rList.size() == 1 && "".equals(rList.get(0)) {
// perform work
}
The line above will only succeed if there's just one entry in the list, and only if it's the empty string. The reverse of the string literal and get
is to guard against a NullPointerException
, as that can occur if you have it flipped the other way 'round, and there's null
in for element 0.
仅当列表中只有一个条目时,上面的行才会成功,并且仅当它是空字符串时。字符串字面量的反向 andget
是为了防止 a NullPointerException
,因为如果您将它翻转过来,并且null
元素 0 中的元素为 0 ,则可能会发生这种情况。
回答by Salahin Rocky
To check empty list use this code: Even list size 1 but its get() method return null. so that you can ensure that actually list have no value.
要检查空列表,请使用以下代码:即使列表大小为 1,但其 get() 方法返回 null。这样您就可以确保实际列表没有任何价值。
if(someList.get(0)!=null)
回答by Hajmola
To check whether a String is empty: String x =""; boolean isXEmpty = x.isEmpty();
检查字符串是否为空: String x =""; boolean isXEmpty = x.isEmpty();
Yes, the empty String instance is a valid instance as any other String. So it will count.
是的,空字符串实例与任何其他字符串一样是有效实例。所以会算的。
回答by Drew Kennedy
To answer your question How can I check this kind of array list?
, you can check the indices to see if your ArrayList contains any particular value by iterating through it.
要回答您的问题How can I check this kind of array list?
,您可以通过遍历索引来检查索引以查看您的 ArrayList 是否包含任何特定值。
ArrayList<String> rList = new ArrayList<String>();
rList.add("");
for (String item : rList) {
if (item.equals("")) {
//do something
}
}
If you're checking if all indices of your ArrayList are empty Strings, one way is to add a counter and compare it to the size of the ArrayList.
如果您正在检查 ArrayList 的所有索引是否都是空字符串,一种方法是添加一个计数器并将其与 ArrayList 的大小进行比较。
ArrayList<String> rList = new ArrayList<String>();
rList.add("");
rList.add("");
rList.add("");
int crossCheck = 0;
for (String item : rList) {
if (item == null) {//equals() will throw a NPE when checking for null values
//do something
} else if (item.isEmpty()) {
crossCheck++;
//do something
}
}
//both are 3 in this case
if (rList.size() == crossCheck) {
//all indices are empty Strings
}
Makoto has an excellent answer for the rest of your question.
Makoto 对您的其余问题有一个很好的回答。