0

my array is like as below:

    unitList = new ArrayList<String>( Arrays.
            asList(result.getJSONObject( position ).
                    getString( "units" ).replace("[", "").
                    replace("]", "").
                    split(",")));

I want to know if it have at least one element in this array, when I use this function:

unitList.size()

it return this:

06-30 02:04:51.453 18653-18653/search.bert.searchviewtest I/unitList.size: 1
06-30 02:04:51.453 18653-18653/search.bert.searchviewtest I/unitList: []
06-30 02:04:56.123 18653-18653/search.bert.searchviewtest I/unitList.size: 6
06-30 02:04:56.123 18653-18653/search.bert.searchviewtest I/unitList: ["1", "2", "3", "4", "5", "6"]

This is very strange, because [] do not have any element, but it return 1. If the list is ["1"], will it also return 1? How can I know if the list is empty or not?

supplement1:

My log code is as below:

    Log.i("unitList.size", String.valueOf( ( unitList.size() ) ) );
    Log.i("unitList",unitList.toString());
3
  • 3
    You've shown log entries, but not how you're performing the logging. It's also unclear what the various values are you in your code. Please could you provide a minimal reproducible example? I doubt that anything here is Android-specific - a simple Java console app should demonstrate the problem easily. Commented Jun 29, 2018 at 18:15
  • You are doing it correctly. Something else is happening that you aren't showing here. Please provide a minimal reproducible example. In this case, you should create a simple Java program without any Android since your question has nothing to do with the Android SDK or API. Commented Jun 29, 2018 at 18:15
  • 3
    Probably, your unitList contains only one item - empty string. Commented Jun 29, 2018 at 18:19

4 Answers 4

2

If you just want to check if there is at least one element in the list you can use the isEmpty() function on the list.

However, if that returns true when it shouldn't (and likely why you are getting 1 when it is empty) you are likely passing in the empty string ""

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

Comments

2

Just use isEmpty() method of ArrayList:

if (unitList.isEmpty()) /* ... */
else /* ... */

I think that it returns 1 because you're splitting the string, with an array containing an empty string as result. (I'm not 100% sure)

Comments

0

You have one empty String element in your array

the fact that this code didnt raise any exceptions means the code had some string that it was working on (mostly empty).

unitList = new ArrayList<String>( Arrays.
        asList(result.getJSONObject( position ).
                getString( "units" ).replace("[", "").
                replace("]", "").
                split(",")));

if truly the split(",") did run on null String it would raise exception so for sure it did run on empty string which resulted on empty string as well

Comments

0

Check if result string is empty before splitting it, otherwise you get a non-empty array with an empty string.

String str = result.getJSONObject(POSITION)
                   .getString("units")
                   .replace("[", "")
                   .replace("]", "");
unitList = str.isEmpty() ? Collections.emptyList() : Arrays.asList(str.split(","));

Comments

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.