Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ArrayList size returning 1 even if no item in the list

I am working with Android. I have an ArrayList of string data and I saved whole Arraylist as a single string in local database.Later I fetch this string from db and convert this string into array list as,

case 1:

  String dbString=[10,5,34,67];  // array list as string
  String replace = dbString.toString().replace("[", "");
            String replace1 = replace.replace("]", "");
            mysignimgList = new ArrayList<String>(Arrays.asList(replace1.split(",")));

case 2:

  String dbString2=[];  //empty array list as string
  String replace = dbString2.toString().replace("[", "");
            String replace1 = replace.replace("]", "");
            mysignimgList2 = new ArrayList<String>(Arrays.asList(replace1.split(",")));

but in the second case even if there is no item in the arraylist ,its size will get as mysignimgList2.size()=1.

like image 958
UserAndroid Avatar asked Oct 24 '25 05:10

UserAndroid


1 Answers

This is a feature of String.split.

The array returned by this method contains each substring of this string that is terminated by another substring that matches the given expression or is terminated by the end of the string.

So splitting an empty string returns a 1-entry array containing one empty string.

like image 137
OldCurmudgeon Avatar answered Oct 26 '25 19:10

OldCurmudgeon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!