我有一个读取文件并存储在arraylist中然后转换为数组的代码(用于表模型)
我的课程正确扩展了 abstracttablemodel。
我的所有代码是:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;
public class ReadFileToList extends AbstractTableModel{
String[] col={"Fname","Lname","Number"};
List<String> data=new ArrayList<String>();
String[][] Arraydata;
public ReadFileToList(){
try{
FileReader fr=new FileReader("D:\\AllUserRecords.txt");
BufferedReader br=new BufferedReader(fr);
String line;
while((line=br.readLine()) !=null){
data.add(line);
}
br.close();
Arraydata=(String[][]) data.toArray();
}
catch(IOException ioe){
ioe.printStackTrace();
}
}
@Override
public String getColumnName(int colu){
return col[colu];
}
public int getRowCount() {
return Arraydata.length;
}
public int getColumnCount() {
return col.length;
}
public Object getValueAt(int rowIndex, int columnIndex) {
return Arraydata[rowIndex][columnIndex];
}
}
我的主要课程是 ReadFileToListM:
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class ReadFileToListM {
ReadFileToList rftl=new ReadFileToList();
public ReadFileToListM(){
JFrame frame=new JFrame();
JTable table=new JTable(rftl);
JPanel panel=new JPanel();
JScrollPane sp=new JScrollPane(table);
panel.add(sp);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(470,470);
frame.setVisible(true);
}
public static void main(String[] args){
new ReadFileToListM();
}
}
但它有例外!
这是我的例外:
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [[Ljava.lang.String;
at javaapplication1.ReadFileToList.<init>(ReadFileToList.java:37)
at javaapplication1.ReadFileToListM.<init>(ReadFileToListM.java:8)
at javaapplication1.ReadFileToListM.main(ReadFileToListM.java:22)
Java Result: 1
我的txt文件:
FName Lname Number
second secondsecond 22
thired thithird 33
fourth fourfourr 44
fifth fiffif 55
请帮助我,谢谢。