gpt4 book ai didi

java - Stringarray 保持 null Java

转载 作者:太空宇宙 更新时间:2023-11-04 10:08:37 24 4
gpt4 key购买 nike

我尝试用 stringTemp 填充 tempArray,但无论出于何种原因,数组中的值始终为 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);
}
}

最佳答案

我猜你正在尝试用这些语句填充数组

if (index < tempArray.length - 1) {
tempArray[index] = stringTemp;
index++;
}

您的意思可能是:

for (int index = 0; index < tempArray.length; index++) {
tempArray[index] = stringTemp;
}

更新:
如果您的索引是静态的,并且您希望每次调用方法时都填充数组的单个元素(至少看起来是这样),那么您可能也想将数组声明为静态。
现在的代码每次调用该方法时都会创建一个新数组。

关于java - Stringarray 保持 null Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52630089/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com