I try to read data from a text file and add the data into an arraylist whose element is 7-number array. But all the elements of the arraylist always turn to the value which is read last time, which means the last data read replace all the elements in the list. I guess the reason might be the function has to be called in the main method which is static. How can I add the data successfully?
package main;
import java.util.Locale;
import java.util.Scanner;
import javax.swing.JFileChooser;
import java.util.ArrayList;
import java.io.*;
public class readFile {
public static void main(String args[]){
read();
}
public void read()throws Exception{
Scanner input = new Scanner(new File("./src/sample.txt"));
input.useLocale(Locale.US);
double[] temp = new double[7];
ArrayList<double[]> pointList= new ArrayList<double[]>();
while(input.hasNext()){
for (int i=0;i<=6;i++)
{
temp[i]=input.nextDouble();
}
pointList.add(temp);
}
}
}
The data in text file is as the bottom in the figure, while the result is just same for each element.

readneeds to bestatic(or invoked vianew readFile().read()). Or just inlinereadintomain.