先具体说下我这个软件中的内容:1、
Access 是一个类,其中含有两个Date型的变量。这个类中有个 toArray的方法将各个成员变量转换成Object[]数组
public class Access {
int no;
String name;
int capboard;
Date start;
Date end;public Object[] toArray()
{
Object[] vars = new Object[5];
vars[0] = this.no;
vars[1] = this.name;
vars[2] = this.capboard;
vars[3] = this.start;
vars[4] = this.end;
return vars;
}
2、我用ArrayList<Access> tbook2=new ArrayList<Access>(); 在内存中存放各个Access对象的实例
3、 我所有的记录信息是通过用 ObjectInputStream、ObjectOutputStream流 读写dat文件完成的。打开我的软件时,先从文件中读取,写入ArrayList<Access> tbook2。 关闭软件时,再将更新后的ArrayList<Access> tbook2 的 内容写入文件private void readFromFile(String filename)
{
try
{
FileInputStream fin=new FileInputStream(filename); //文件字节输入流
ObjectInputStream objin=new ObjectInputStream(fin); //对象字节输入流
while (true) //输入流未结束时
try
{
tbook2.add((Access)objin.readObject());//读取一个对象添加到tbook2
}
catch (Exception e) //捕获ClassNotFoundException和EOFException异常
{
break;
}
objin.close(); //先关闭对象流
fin.close(); //再关闭文件流
}
catch (IOException ioe){} //指定文件不存在时,表格为空
}
public void windowClosing(WindowEvent e) //点击窗口关闭按钮时触发并执行
{
//将修改后的信息写入文件
try
{
FileOutputStream fout=new FileOutputStream(filename); //文件字节输出流
ObjectOutputStream objout=new ObjectOutputStream(fout); //对象字节输出流
if (!tbook2.isEmpty())
{
Iterator it = tbook2.iterator();
while (it.hasNext())
{
objout.writeObject((Access)it.next()); //写入一个对象
}
}
objout.close();
fout.close();
}
catch (IOException ioe){}
}4、我输入一条 Access信息,是在我写的一个图形化界面中,里面有文本框。然后这样给ArrayList<Access> tbook2 添加一条记录Access temp=new Access(Integer.parseInt(no.getText()),name.getText(),Integer.parseInt(capboard.getText()),new SimpleDateFormat("yyyy-MM-dd").parse(start.getText()),new SimpleDateFormat("yyyy-MM-dd").parse(end.getText()));
tbook2.add(temp);
————————————————————————————————————————————我要实现的是,当在 显示各种信息的JTable修改某个单元格的值后。能找到这一行在ArrayList<Access> tbook2中的位置,然后用int ChangedRow=tbook.indexOf(Access_last);
tbook2.set(ChangedRow,Access_fix);来实现更新ArrayList<Access> tbook2中的数据其中,我已经建好了这样的监视器,只要双击某一行,就先备份下此行的信息。如果最终修改了此行,就利用此备份信息使得int ChangedRow=tbook.indexOf(Access_last); 能找到原来的信息在tbook2中的存储位置//添加表格中的鼠标点击监视器,当点击一行时先将此行内容备份
table.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
temp=(table.getValueAt(table.getSelectedRow(),table.getSelectedColumn())).toString();
//table.getValueAt返回值为Object,要让它最终变成int,先将其toSting(),然后用Integer(String s)专转成Integer类,再用Integer类的intValue()方法转成int
try
{
Access_last=new Access(
(new Integer(table.getValueAt(table.getSelectedRow(),0).toString()).intValue()),
(table.getValueAt(table.getSelectedRow(),1)).toString(),
(new Integer(table.getValueAt(table.getSelectedRow(),2).toString()).intValue()),
(Date) table.getValueAt(table.getSelectedRow(),3),
(Date) table.getValueAt(table.getSelectedRow(),4)
);
}
catch(Exception e1){System.out.println("无法新建");}
}
});
——————————————————————————————————————————但是结果却不是我想要的。int ChangedRow=tbook.indexOf(Access_last); 这一句实际在tbook2中是找不到原来信息对应在tbook2的位置的,也就是说返回值为-1我不知道为何会这样?请大家给出以下解决方案中的至少一种吧:1、最好是找到为何我这种方法不行,然后以最小的修改做成
2、如果有其他方案也可以提示:1、不要用 tbook2.remove(e.getLastRow()); tbook2.add(Access_fix); 我就是要更新原来的位置,不要删除后新建2、我的另一个类,里面更简单,全都是String,用这个办法都不管用public class Student
{
String name;
String gender;
String tel;
String QQ;
String mail;
String academic;
String re;
}3、hashCode函数,equals函数,我都用eclipse自动生成了
Access 是一个类,其中含有两个Date型的变量。这个类中有个 toArray的方法将各个成员变量转换成Object[]数组
public class Access {
int no;
String name;
int capboard;
Date start;
Date end;public Object[] toArray()
{
Object[] vars = new Object[5];
vars[0] = this.no;
vars[1] = this.name;
vars[2] = this.capboard;
vars[3] = this.start;
vars[4] = this.end;
return vars;
}
2、我用ArrayList<Access> tbook2=new ArrayList<Access>(); 在内存中存放各个Access对象的实例
3、 我所有的记录信息是通过用 ObjectInputStream、ObjectOutputStream流 读写dat文件完成的。打开我的软件时,先从文件中读取,写入ArrayList<Access> tbook2。 关闭软件时,再将更新后的ArrayList<Access> tbook2 的 内容写入文件private void readFromFile(String filename)
{
try
{
FileInputStream fin=new FileInputStream(filename); //文件字节输入流
ObjectInputStream objin=new ObjectInputStream(fin); //对象字节输入流
while (true) //输入流未结束时
try
{
tbook2.add((Access)objin.readObject());//读取一个对象添加到tbook2
}
catch (Exception e) //捕获ClassNotFoundException和EOFException异常
{
break;
}
objin.close(); //先关闭对象流
fin.close(); //再关闭文件流
}
catch (IOException ioe){} //指定文件不存在时,表格为空
}
public void windowClosing(WindowEvent e) //点击窗口关闭按钮时触发并执行
{
//将修改后的信息写入文件
try
{
FileOutputStream fout=new FileOutputStream(filename); //文件字节输出流
ObjectOutputStream objout=new ObjectOutputStream(fout); //对象字节输出流
if (!tbook2.isEmpty())
{
Iterator it = tbook2.iterator();
while (it.hasNext())
{
objout.writeObject((Access)it.next()); //写入一个对象
}
}
objout.close();
fout.close();
}
catch (IOException ioe){}
}4、我输入一条 Access信息,是在我写的一个图形化界面中,里面有文本框。然后这样给ArrayList<Access> tbook2 添加一条记录Access temp=new Access(Integer.parseInt(no.getText()),name.getText(),Integer.parseInt(capboard.getText()),new SimpleDateFormat("yyyy-MM-dd").parse(start.getText()),new SimpleDateFormat("yyyy-MM-dd").parse(end.getText()));
tbook2.add(temp);
————————————————————————————————————————————我要实现的是,当在 显示各种信息的JTable修改某个单元格的值后。能找到这一行在ArrayList<Access> tbook2中的位置,然后用int ChangedRow=tbook.indexOf(Access_last);
tbook2.set(ChangedRow,Access_fix);来实现更新ArrayList<Access> tbook2中的数据其中,我已经建好了这样的监视器,只要双击某一行,就先备份下此行的信息。如果最终修改了此行,就利用此备份信息使得int ChangedRow=tbook.indexOf(Access_last); 能找到原来的信息在tbook2中的存储位置//添加表格中的鼠标点击监视器,当点击一行时先将此行内容备份
table.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
temp=(table.getValueAt(table.getSelectedRow(),table.getSelectedColumn())).toString();
//table.getValueAt返回值为Object,要让它最终变成int,先将其toSting(),然后用Integer(String s)专转成Integer类,再用Integer类的intValue()方法转成int
try
{
Access_last=new Access(
(new Integer(table.getValueAt(table.getSelectedRow(),0).toString()).intValue()),
(table.getValueAt(table.getSelectedRow(),1)).toString(),
(new Integer(table.getValueAt(table.getSelectedRow(),2).toString()).intValue()),
(Date) table.getValueAt(table.getSelectedRow(),3),
(Date) table.getValueAt(table.getSelectedRow(),4)
);
}
catch(Exception e1){System.out.println("无法新建");}
}
});
——————————————————————————————————————————但是结果却不是我想要的。int ChangedRow=tbook.indexOf(Access_last); 这一句实际在tbook2中是找不到原来信息对应在tbook2的位置的,也就是说返回值为-1我不知道为何会这样?请大家给出以下解决方案中的至少一种吧:1、最好是找到为何我这种方法不行,然后以最小的修改做成
2、如果有其他方案也可以提示:1、不要用 tbook2.remove(e.getLastRow()); tbook2.add(Access_fix); 我就是要更新原来的位置,不要删除后新建2、我的另一个类,里面更简单,全都是String,用这个办法都不管用public class Student
{
String name;
String gender;
String tel;
String QQ;
String mail;
String academic;
String re;
}3、hashCode函数,equals函数,我都用eclipse自动生成了
附上代码,都是eclipse自动生成的呀
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + capboard;
result = prime * result + ((end == null) ? 0 : end.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + no;
result = prime * result + ((start == null) ? 0 : start.hashCode());
return result;
} @Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Access other = (Access) obj;
if (capboard != other.capboard)
return false;
if (end == null) {
if (other.end != null)
return false;
} else if (!end.equals(other.end))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (no != other.no)
return false;
if (start == null) {
if (other.start != null)
return false;
} else if (!start.equals(other.start))
return false;
return true;
}