题目:
以下程序输出什么?
public class OJ {
public static void main(String[] args) {
String s="";
System.out.println(s.split(",").length); //空字符串分割
}
}
我们看源代码:
public String[] split(String regex, int limit) {
/* fastpath if the regex is a
**其它代码-***/
// If no match was found, return this -----重点
if (off == 0)
return new String[]{this};
// Add remaining segment
if (!limited || list.size() < limit)
list.add(substring(off, value.length));
// Construct result
int resultSize = list.size();
if (limit == 0) {
while (resultSize > 0 && list.get(resultSize - 1).length() == 0) {
resultSize--;
}
}
/***其它代码***/
}
按原代码来说 返回的是一个字符串数组,这个数组的第一个位置存着:“” 。所以输出类似以下代码:
String[] a=new String[]{""};
System.out.println(a.length);
//输出为 1