我正在尝试使此循环将文件中的每一行放入一个对象,然后将每个对象存储在arrayList中。出于某种原因,循环正确存储了输入的前三行,但是当到达最后一行时,数组列表中的每个项目都变为最后一行中的值。我在下面粘贴了代码的相关部分,有什么建议吗?
while (lineIteration != null) {
//store polynomial objects from Polynomial class in an array list.
ta.setText(ta.getText() + lineIteration + "\n"); //prints the input to
// the text area
Polynomial objLine = new Polynomial(lineIteration); //store a line in the
// objLine object
System.out.println(objLine);
polyArrayList.add(objLine); //add the object to the arrayList
lineIteration = inBR.readLine(); //read the next lineIteration.
}
完整代码如下。 / ***** CLASS Main.java ***** /
import java.util.Iterator;
public class Polynomial implements Iterable, Comparable{
private static String poly;
public Polynomial (String x){ // constructor that accepts the input and stores it in a polynomial.
poly = x;
return;
}
@Override
public String toString() {
return poly;
}
private static class Node<String>{
private String data;
private Node<String> next;
public Node (String data, Node<String> next){
this.data = data;
this.next = next;
}
}
@Override
public Iterator iterator() {
return null;
}
@Override
public int compareTo(Object o) {
return 0;
}
}
/****Polynomial.java****/
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.io.*;
import java.awt.event.ActionListener;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.logging.Logger;
public class Main extends JFrame implements ActionListener {
JMenuBar mb;
JMenu file;
JMenuItem open;
JTextArea ta;
static ArrayList<Polynomial> polyArrayList = new ArrayList<>(); //ArrayList of objects Polynomial
Main(){
open = new JMenuItem("Open File");
open.addActionListener(this);
file = new JMenu("File");
file.add(open);
mb = new JMenuBar();
mb.setBounds(0,0,800,20);
mb.add(file);
ta = new JTextArea(800,800);
ta.setBounds(0,20,800,800);
add(mb);
add(ta);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == open) {
JFileChooser fc = new JFileChooser();
int i = fc.showOpenDialog(this);
if (i == JFileChooser.APPROVE_OPTION) {
File selectedFile = fc.getSelectedFile();
String filepath = selectedFile.getPath();
try {
BufferedReader inBR;
inBR = new BufferedReader(new FileReader(selectedFile));
String lineIteration = inBR.readLine();
while (lineIteration != null) {
//store polynomial objects from Polynomial class in an array list.
ta.setText(ta.getText() + lineIteration + "\n"); //prints the input to the text area
Polynomial objLine = new Polynomial(lineIteration); //store a line in the objLine object
System.out.println(objLine);
polyArrayList.add(objLine); //add the object to the arrayList
lineIteration = inBR.readLine(); //read the next lineIteration.
}
} catch (FileNotFoundException fileNotFoundException) {
System.out.println("File not Found");
} catch (IOException ioException) {
System.out.println("IO Exception");
}
System.out.println(polyArrayList.get(0));
System.out.println(polyArrayList.get(1));
System.out.println(polyArrayList.get(2));
System.out.println(polyArrayList.get(3));
}
}
}
public static void main(String[] args){
System.out.println("Start");
Main om = new Main();
om.setSize(500,500);
om.setLayout(null);
om.setVisible(true);
om.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
答案 0 :(得分:2)
poly
变量具有static
修饰符,表示该变量在该类的所有实例之间共享。当在Polynomial
对象的另一个实例中将变量设置为新值时,这也将反映在所有其他Polynomial
对象中。
由于文件的最后一行是在变量上设置的最新值,因此它将是所有Polynomial
对象上设置的值。只需删除变量上的static
。
答案 1 :(得分:2)
您必须从poly
中的Polynomial
中删除静态变量:
public class Polynomial implements Iterable, Comparable {
private String poly;
public Polynomial(String poly) { // constructor that accepts the input and stores it in a polynomial.
this.poly = poly;
}
static
关键字表示所有实例将在它们之间共享完全相同的值。即使您没有看到,在每次迭代中,您都将所有实例中的poly
值设置为相同的值。您可以进一步了解static
关键字here。