我有一個至少有三個元素的鋸齒狀數組,我需要解析出前五個元素,用空格填充任何空值。如何在Java中將鋸齒形數組解析爲單個變量?
// there will ALWAYS be three elements
String whiconcatC = scrubbedInputArray[0];
String whiconcatD = scrubbedInputArray[1];
String whiconcatE = scrubbedInputArray[2];
// there MAY be a fourth or fifth element
if (scrubbedInputTokens > 3) {
String whiconcatF = scrubbedInputArray[3];
} else {
String whiconcatF = " ";
}
//
if (scrubbedInputTokens > 4) {
String whiconcatG = scrubbedInputArray[4];
} else {
String whiconcatG = " ";
}
雖然上面的代碼不會產生編譯過程中出現錯誤,隨後的行中引用whiconcatF
或whiconcatG
將錯誤輸出與cannot find symbol
期間編譯。
我使用forEach
和StringTokenizer
(數組轉換成字符串分隔後)嘗試過,但無法弄清楚如何在工作情況下的默認值是有斑點沒有價值4 & 5.
我一直無法找出任何其他方式來做到這一點,也沒有爲什麼我的邏輯如果失敗。建議?
謝謝,大衛,它做到了。範圍問題。 – dwwilson66
你可能想要初始化它們,以避免空指針異常。 –
@david - 您應該避免編輯問題,以便修復錯誤本身。這會使下面給出的答案完全不相關。 – SudoRahul