我正在尝试将.txt文件中的一行读入一维数组,再添加两个元素到基于该数组的数据,并将新的和更大的数据存储在名为“dataArrayList”的2D ArrayList中,然后重复.txt文件中的后续行。
public static ArrayList<String[]> dataArrayList = new ArrayList<>();
public static void readFile() throws Exception {
fileRead = new FileReader(txtFileLocation);
dataReader = new BufferedReader(fileRead);
String line = "";
String[] temp;
String[] temp2 = new String[15];
//reading the text file
for (int i = 0; line != null; i++) {
line = dataReader.readLine();
if (line != null) {
//line has 13 Strings to split
temp = line.split(",");
//making a larger duplicate with 15 indices instead of 13 as to add 2 more Strings
System.arraycopy(temp, 0, temp2, 0, temp.length);
/*
my code to assign values to the two new
elements based data from the previous ones
*/
//adding temp2 to my ArrayList
dataArrayList.add(temp2);
}
}
}
它似乎应该可以工作,但是当我之后访问dataArrayList时,无论是打印出来还是将其放入JTable,所有记录都是.txt文件最后一行的副本。
例如,如果.txt文件看起来像这样(对于非预期的语法突出显示道歉):
Hello,my,name,is,Hunter,how,are,you,doing,on,a,lovely,day,like,this
Have,you,heard,about,the,weather,we're,supposed,to,get,a,lot,of,rain,later
Hopefully,it's,sunny,for,the,entire,day,tomorrow,I,have,a,soccer,game,that,day
第一次添加记录时,dataArrayList看起来像你期望的那样,如下所示:
Hello,my,name,is,Hunter,how,are,you,doing,on,a,lovely,day,like,this
然而,在它第二次运行并“添加”第二行后,它看起来像这样:
Have,you,heard,about,the,weather,we're,supposed,to,get,a,lot,of,rain,later
Have,you,heard,about,the,weather,we're,supposed,to,get,a,lot,of,rain,later
最后,在添加第三条记录后,它看起来像这样:
Hopefully,it's,sunny,for,the,entire,day,tomorrow,I,have,a,soccer,game,that,day
Hopefully,it's,sunny,for,the,entire,day,tomorrow,I,have,a,soccer,game,that,day
Hopefully,it's,sunny,for,the,entire,day,tomorrow,I,have,a,soccer,game,that,day
我不明白发生了什么......看起来它正在用当前的记录覆盖以前的记录。
答案 0 :(得分:0)
添加temp2 = new String[15];
作为循环的最后一行
for (int i = 0; line != null; i++) {
line = dataReader.readLine();
if (line != null) {
//line has 13 Strings to split
temp = line.split(",");
//making a larger duplicate with 15 indices instead of 13 as to add 2 more Strings
System.arraycopy(temp, 0, temp2, 0, temp.length);
/*
my code to assign values to the two new
elements based data from the previous ones
*/
//adding temp2 to my ArrayList
dataArrayList.add(temp2);
temp2 = new String[15];
}
}
答案 1 :(得分:0)
以下代码行修改变量temp2的内容
<?php
$type=htmlspecialchars($_POST["type"]);
if ($type=="1")
{
echo "1-one\n";//Ends-with \n for client side getting data from server side response
echo "3-three\n";//Ends-with \n for client side getting data from server side response
echo "5-five\n";//Ends-with \n for client side getting data from server side response
echo "7-seven\n";//Ends-with \n for client side getting data from server side response
}
else
{
if ($type=="0")
{ echo "2-two\n";//Ends-with \n for client side getting data from server side response
echo "4-four\n";//Ends-with \n for client side getting data from server side response
echo "6-six\n";//Ends-with \n for client side getting data from server side response
echo "8-eight\n";//Ends-with \n for client side getting data from server side response
}
}
?>
您将相同的变量连续添加到列表
System.arraycopy(temp, 0, temp2, 0, temp.length);
这就是为什么temp2包含文本文件的最后一行,而list只是重复同一行。
我建议
dataArrayList.add(temp2);