Java从数组中检索对象
从所述对象的数组中获取对象时遇到问题。我的代码如下。我基本上是试图构建一个表,并用每个对象的数据填充列。但是,当前表只是显示对象,而不是数据。见图: 表:Java从数组中检索对象,java,arrays,object,jtable,Java,Arrays,Object,Jtable,从所述对象的数组中获取对象时遇到问题。我的代码如下。我基本上是试图构建一个表,并用每个对象的数据填充列。但是,当前表只是显示对象,而不是数据。见图: 表: public void buildTable(JPanel panel) { File file = new File("C:\\Users\\user\\Desktop\\test.txt"); try { BufferedReader br = new BufferedReader(new FileRe
public void buildTable(JPanel panel) {
File file = new File("C:\\Users\\user\\Desktop\\test.txt");
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
lineArray = new ArrayList<Person[]>();
while((line = br.readLine()) != null) {
String[] temp = line.split("\\t");
Person p = new Person(temp[0], temp[1], temp[2], temp[3], temp[4], temp[5], temp[6], temp[7], temp[8]);
lineArray.add(new Person[]{p});
}
br.close();
} catch (FileNotFoundException e) { e.printStackTrace();}
catch (IOException e) { e.printStackTrace(); }
List<String> columns = new ArrayList<String>();
final List<Person[]> values = new ArrayList<Person[]>();
columns.add("First Name");
columns.add("Last Name");
columns.add("Address");
columns.add("Address 2");
columns.add("City");
columns.add("State");
columns.add("Zip Code");
columns.add("Phone");
columns.add("Email");
for (int i = 0; i < lineArray.size(); i++) {
values.add(lineArray.get(i)); //this is the line where I'm guessing the main problem is
}
TableModel tableModel = new DefaultTableModel(values.toArray(new Object[][] {}), columns.toArray());
final JTable table = new JTable(tableModel);
JScrollPane tableContainer = new JScrollPane(table);
tableContainer.setBounds(10, 36, 833, 219);
panel.add(tableContainer);
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
int selectedRow = table.convertRowIndexToModel(table.getSelectedRow());
contactName.setText((String) table.getValueAt(selectedRow, 0) + " " + table.getValueAt(selectedRow, 1));
contactAddress.setText((String) table.getValueAt(selectedRow, 2));
}
});
}尝试将字符串数组添加到
lineArray
而不是Person
对象中
lineArray.add(new String[]{temp[0], temp[1], temp[2], temp[3], temp[4], temp[5], temp[6], temp[7], temp[8]});
要解决您的问题,您只需要一个Person数组列表(而不需要Person数组),并且不需要值ArrayList 相反,您需要的是创建一个数组数组的方法,该数组表示包含数据的表,实际上是一个字符串数组数组。问题是表的“列”实际上是Person类的字段。我不确定我的解决方案(如下)是否是最好的,也许你也可以解决这个问题,处理反射 无论如何,这是可行的(我尽量少修改代码或简化代码):
公共void构建表(JPanel面板)引发IOException{
File File=新文件(“C:\\Users\\user\\Desktop\\test.txt”);
试一试{
BufferedReader br=新的BufferedReader(新文件读取器(文件));
弦线;
lineArray=新的ArrayList();
而((line=br.readLine())!=null){
String[]temp=line.split(“,”);
人员p=新人员(临时[0]、临时[1]、临时[2]、临时[3]、临时[4]、临时[5]、临时[6]、临时[7]、临时[8]);
线性排列。添加(p);
}
br.close();
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
列表列=新的ArrayList();
列。添加(“名字”);
列。添加(“姓氏”);
列。添加(“地址”);
列。添加(“地址2”);
列。添加(“城市”);
列。添加(“州”);
列。添加(“邮政编码”);
列。添加(“电话”);
列。添加(“电子邮件”);
TableModel TableModel=新的DefaultTableModel(Person.toString矩阵(lineArray),columns.toArray());
最终JTable表格=新JTable(表格模型);
JScrollPane tableContainer=新的JScrollPane(表);
tableContainer.setBounds(10,36,833,219);
面板。添加(tableContainer);
}
以及个人类别:
class Person {
public String firstName;
public String lastName;
public String address;
public String address2;
public String city;
public String state;
public String zip;
public String phone;
public String email;
public Person(String firstName, String lastName, String address, String address2, String city, String state, String zip, String phone, String email) {
this.firstName = firstName;
this.lastName = lastName;
this.address = address;
this.address2 = address2;
this.city = city;
this.state = state;
this.zip = zip;
this.phone = phone;
this.email = email;
}
public String[] toStrings() {
return new String[]{firstName, lastName, address, address2, city, state, zip, phone, email};
}
public static String[][] toStringsMatrix(ArrayList<Person> personList) {
int rows = personList.size();
String[] first = personList.get(0).toStrings();
int columns = first.length;
String[][] table = new String[rows][columns];
table[0] = first;
for (int k = 1; k < rows; k++) {
table[k] = personList.get(k).toStrings();
}
return table;
}
}
班级人员{
公共字符串名;
公共字符串lastName;
公共字符串地址;
公共字符串地址2;
公共字符串城市;
公共字符串状态;
公共字符串压缩;
公用串电话;
公共字符串电子邮件;
公众人物(字符串名、字符串名、字符串地址、字符串地址2、字符串城市、字符串州、字符串邮编、字符串电话、字符串电子邮件){
this.firstName=firstName;
this.lastName=lastName;
this.address=地址;
this.address2=address2;
this.city=城市;
this.state=状态;
this.zip=zip;
this.phone=电话;
this.email=电子邮件;
}
公共字符串[]toStrings(){
返回新字符串[]{firstName,lastName,address,address2,city,state,zip,phone,email};
}
公共静态字符串[][]ToString矩阵(ArrayList personList){
int rows=personList.size();
String[]first=personList.get(0.ToString();
int columns=first.length;
字符串[][]表=新字符串[行][列];
表[0]=第一;
对于(int k=1;k
我希望这能对您有所帮助。我想在以后使用该对象,以便更方便地访问,因为我必须在不同的区域传递信息和提取信息。查看您的代码看起来您有设计问题
lineArray
是Person
数组的数组列表<代码>线性阵列=新阵列列表()代码>我想您只需要将Person对象添加到数组列表中即可。我正在将Person对象添加到arraylistlineArray.add(newPerson[]{p})代码>长话短说<代码>线性阵列。添加(p)
并将arraylist定义为lineArray=newArrayList()代码>非常感谢您的帮助,我非常感谢您的帮助!
public void buildTable(JPanel panel) throws IOException {
File file = new File("C:\\Users\\user\\Desktop\\test.txt");
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
lineArray = new ArrayList<Person>();
while ((line = br.readLine()) != null) {
String[] temp = line.split(",");
Person p = new Person(temp[0], temp[1], temp[2], temp[3], temp[4], temp[5], temp[6], temp[7], temp[8]);
lineArray.add(p);
}
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
List<String> columns = new ArrayList<String>();
columns.add("First Name");
columns.add("Last Name");
columns.add("Address");
columns.add("Address 2");
columns.add("City");
columns.add("State");
columns.add("Zip Code");
columns.add("Phone");
columns.add("Email");
TableModel tableModel = new DefaultTableModel(Person.toStringsMatrix(lineArray), columns.toArray());
final JTable table = new JTable(tableModel);
JScrollPane tableContainer = new JScrollPane(table);
tableContainer.setBounds(10, 36, 833, 219);
panel.add(tableContainer);
}
class Person {
public String firstName;
public String lastName;
public String address;
public String address2;
public String city;
public String state;
public String zip;
public String phone;
public String email;
public Person(String firstName, String lastName, String address, String address2, String city, String state, String zip, String phone, String email) {
this.firstName = firstName;
this.lastName = lastName;
this.address = address;
this.address2 = address2;
this.city = city;
this.state = state;
this.zip = zip;
this.phone = phone;
this.email = email;
}
public String[] toStrings() {
return new String[]{firstName, lastName, address, address2, city, state, zip, phone, email};
}
public static String[][] toStringsMatrix(ArrayList<Person> personList) {
int rows = personList.size();
String[] first = personList.get(0).toStrings();
int columns = first.length;
String[][] table = new String[rows][columns];
table[0] = first;
for (int k = 1; k < rows; k++) {
table[k] = personList.get(k).toStrings();
}
return table;
}
}