Android: Can Split method return null?
I am having a problem reading this code.
Why allProvinces = null
does the encoder determine what ?
if(!TextUtils.isEmpty(response)){
String [] allProvinces = response.split(",");
/* how should I check whether allProvinces is null or not?
* it means the method "split" can possibly return a null value?
*/
if( allProvinces != null && allProvinces.length > 0){
for(String p :allProvinces) {
String [] array = p.split("//|");
Province province = new Province();
province.setProvinceName(array [0]);
province.setProvinceCode(array[1]);
weatherDb.saveProvince(province);
}
}
}
+3
source to share
2 answers
Can Split method return null?
The split function always returns String[]
containing at least one element.
You end up with a size 1 array containing the original value:
splitting method with "-"
Input Output
----- ------
ABCD-XYZ {"ABCD", "XYZ"}
QWERT {"QWERT"}
ZXC-q- {"ZXC","q"}
ZXC-q- - {"ZXC","q",""}
why does the encoder define allProvinces = null
It's useless to check null
because the split function always returns String[]
containing at least one element.
+2
source to share