我對以下代碼感到困惑,因爲我認爲數組的長度(allCommands
)在沒有任何內容時會爲0。Java:爲什麼數組的長度爲1時什麼都沒有了
字符串test
只有英鎊符號,然後我得到後面的子字符串,然後與#
拆分。
String test = "#";
int beginIndex = test.indexOf("#");
test = test.substring(beginIndex+1);
String[] allCommands = test.split("#");
System.out.println("allCommands length: " + allCommands.length); // output: 1
System.out.println("allCommands array: " + Arrays.toString(allCommands)); // output: []
有人可以解釋這一點嗎?謝謝!
裏面有東西 - 它是一個零長度的字符串。 –