How to know if array list is empty in java?


How to know if array list is empty in java?



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());





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, Complete, and Verifiable example? I doubt that anything here is Android-specific - a simple Java console app should demonstrate the problem easily.
– Daisy Shipton
Jun 29 at 18:15





You are doing it correctly. Something else is happening that you aren't showing here. Please provide a Minimal, Complete, and Verifiable 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.
– Code-Apprentice
Jun 29 at 18:15





Probably, your unitList contains only one item - empty string.
– Timur Levadny
Jun 29 at 18:19




4 Answers
4



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



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)



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



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(","));






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Opening a url is failing in Swift

Export result set on Dbeaver to CSV