简体   繁体   English

带有空元素[]的Java ArrayList大小

[英]Java ArrayList size with empty element []

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而不是空吗?

Even though you're adding an empty string to your list, you're still adding something to 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 sort of 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. 字符串文字和get的反向是防止NullPointerException ,因为如果你将它翻转为另一种方式,那么就会发生这种情况,而元素0则为null

To check whether a String is empty: String x =""; 检查String是否为空:String x =“”; boolean isXEmpty = x.isEmpty(); boolean isXEmpty = x.isEmpty();

Yes, the empty String instance is a valid instance as any other String. 是的,空String实例是任何其他String的有效实例。 So it will count. 所以它会计数。

To check empty list use this code: Even list size 1 but its get() method return null. 要检查空列表,请使用以下代码:即使列表大小为1,但其get()方法仍返回null。 so that you can ensure that actually list have no value. 这样你就可以确保实际列表没有价值。

if(someList.get(0)!=null)

To answer your question How can I check this kind of array list? 回答你的问题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. ,您可以通过遍历它来检查索引以查看您的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对你的其余问题有一个很好的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM