我想從我的列表中刪除重複項,然後嘗試將其保存在一個字符串數組中。但是,我想消除,如果有一個空值,即「」但我的條件似乎不起作用,它仍然顯示在輸出中的空白。我怎樣才能擺脫它?我忽略空白值後,預計8種元素的整體尺寸如何擺脫Java中的數組字符串的空值?
Output:Printing Set Peardonville and South Fraser
Printing Set Clearbrook and South Fraser
Printing Set George Ferguson and Gladwin
Printing Set Ar. Bourquin Exchange
Printing Set Lv. Bourquin Exchange
Printing Set Downtown Abbotsford
Printing Set Marshall and McCallum
Printing Set University of the Fraser Valley
**Printing Set**
StopNamesForDB的大小= 9
Set<String> set = new LinkedHashSet<String>(StopNames);
System.out.println("Size of Set Object" + set.size());
StopNamesForDB = new String[set.size()];
for (String string : set) {
if(string.equals(" ")){
}
else{
System.out.println("Printing Set " + string);
StopNamesForDB[i] = string;
i++;
}
}
務必檢查NPE在(string!= NULL){如果(string.trim ().isEmpty()){}} –
你並不總是要防守。也許他之前沒有任何'null'創建''StopNames'' ......這裏並不重要。 –
它只是一個建議和標準做法。它只會使代碼安全。 –