5

I have one ArrayList, for some reason i have to add empty string element "":

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.

How can I check this kind of array list? does the empty element cause the size equals 1 and not empty?

5
  • 1
    Yes, in mathematics a set which consists of only an empty set is empty. Here the collection doesn't care about the value of the string, but about its presence. Commented Jan 6, 2015 at 21:02
  • 2
    Wait, what? The set of the empty set is nonempty in mathematics; e.g. it's 1 in en.wikipedia.org/wiki/… . Commented Jan 6, 2015 at 21:05
  • What do you mean How can I check this kind of array list? Commented Jan 6, 2015 at 21:18
  • @DrewKennedy, I mean I want to check the array list only contains "" element, if it does, I will do something. Commented Jan 6, 2015 at 21:28
  • I added an answer that pertains to that portion of your question. Commented Jan 6, 2015 at 21:35

4 Answers 4

11

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).

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.

Sign up to request clarification or add additional context in comments.

Comments

2

To check whether a String is empty: String x =""; boolean isXEmpty = x.isEmpty();

Yes, the empty String instance is a valid instance as any other String. So it will count.

1 Comment

Thanks, I think i can follow your suggestion.
2

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.

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

1 Comment

This might still throw an IndexOutOfBoundsException though if the list contains no elements.
0

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.

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<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.

3 Comments

What happens if the list contains a null entry?
Nowhere is an index being null mentioned in the question.
Well no, but that's not my point. We've established that a list can contain null and be non-empty; if you want to be certain that you don't get a NullPointerException, you have to reorder the equals.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.