我想,以填補tempArray同stringTemp,但無論出于何種原因,數組中的值始終null。這可能是我忽略了一些愚蠢的事情,但我就是找不到它。有沒有人看到我做錯了什么或為什么tempArray為空?這是我的代碼: String tempData[]; String[] tempArray = new String[43]; String stringTemp; String tempString; stringTemp = Integer.toString(dwdmChannel) + "," + Short.toString(ADCValue); if (index < tempArray.length - 1) { tempArray[index] = stringTemp; index++; } if( dwdmChannel == 61) { try { CSVWriter writer = new CSVWriter(new FileWriter(filePath)); for (int x = 0; x < tempArray.length - 1; x++) { tempString = tempArray[x]; tempData = tempString.split(","); writer.writeNext(tempData); } writer.close(); } catch (IOException e) { System.out.println(e); } }
1 回答

白衣染霜花
TA貢獻1796條經驗 獲得超10個贊
我猜你正試圖用這些語句填充數組
if (index < tempArray.length - 1) {
tempArray[index] = stringTemp;
index++;
}
你可能的意思是:
for (int index = 0; index < tempArray.length; index++) {
tempArray[index] = stringTemp;
}
更新:
如果您的索引是靜態的,并且您想在每次調用您的方法時填充數組的單個元素(至少,它看起來是這樣),那么您可能也想聲明您的數組是靜態的。
現在的代碼每次調用該方法時都會創建一個新數組。
添加回答
舉報
0/150
提交
取消