【发布时间】:2020-09-29 06:19:36
【问题描述】:
我试图让这个循环将文件中的每一行放入一个对象中,然后将每个对象存储在一个 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.
}
完整的代码如下。 /***** 类 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);
}
}
【问题讨论】:
-
你能分享更多的代码吗?
ta是什么?或者lineIteration或者polyArrayList是怎么实例化的? -
我添加了完整的代码。请原谅混乱,这是一个在制品。
标签: java loops object arraylist