java中怎么创建多个类_java 怎样处理对一个类创建多个对象

在尝试将多个学生对象导出到Excel时遇到问题,所有数据都被显示为最后一条记录。经过调试发现,创建的多个student对象实际上只有一份,导致在外部修改对象值时,list中的值也会随之改变。解决办法是确保每个对象实例都是独立的,并在添加到list时正确保存其状态。代码中展示了如何创建和填充student对象到list,然后写入Excel工作表的过程。

我编写了一下的程序想将多个对象导到excel中但是导出成功了,不过值都变成最后一条的数据通过debug我发现我创建的多个student对象其实只有一个而且对于插入到list中的对象在之后的外部...

我编写了一下的程序 想将多个对象导到excel中 但是导出成功了,不过值都变成最后一条的数据 通过debug我发现 我创建的多个student对象 其实只有一个 而且对于插入到list中的对象 在之后的外部对象值改变的时候 list中的值也跟着改变了 该怎么处理啊

代码:

package action;

import java.io.OutputStream;

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

import model.student;

import jxl.Workbook;

import jxl.write.*;

public class testAction {

public void exportStu(OutputStream os) throws Exception {

try {

WritableWorkbook wbook = Workbook.createWorkbook(os); //建立excel文件

WritableSheet wsheet = wbook.createSheet("学生信息表", 0); //工作表名称

//设置Excel字体

WritableFont wfont = new WritableFont(WritableFont.ARIAL, 16,

WritableFont.BOLD, false,

jxl.format.UnderlineStyle.NO_UNDERLINE,

jxl.format.Colour.BLACK);

WritableCellFormat titleFormat = new WritableCellFormat(wfont);

String[] title = { "学号", "姓名", "性别", "日期" };

//设置Excel表头

for (int i = 0; i < title.length; i++) {

Label excelTitle = new Label(i, 0, title[i], titleFormat);

wsheet.addCell(excelTitle);

}

int c = 1; //用于循环时Excel的行号

student stu = new student();

student stu2 = new student();

student stu3 = new student();

student stu4 = new student();

List list = new ArrayList(); //这个是从数据库中取得要导出的数据

stu.setID(10);

stu.setName("小丁");

stu.setGender("1");

stu.setDate("2012.12.12");

list.add(stu);

stu2.setID(11);

stu2.setName("阿花");

stu2.setGender("0");

stu2.setDate("2012.12.12");

list.add(stu2);

stu3.setID(12);

stu3.setName("花花");

stu3.setGender("0");

stu3.setDate("2012.12.12");

list.add(stu3);

stu4.setID(13);

stu4.setName("大饼");

stu4.setGender("1");

stu4.setDate("2012.12.12");

list.add(stu4);

Iterator it = list.iterator();

while (it.hasNext()) {

stu = (student) it.next();

Label content1 = new Label(0, c, Integer.toString(stu.getID()));

Label content2 = new Label(1, c, stu.getName());

Label content3 = new Label(2, c, stu.getGender().toString());

Label content4 = new Label(3, c, stu.getDate());

wsheet.addCell(content1);

wsheet.addCell(content2);

wsheet.addCell(content3);

wsheet.addCell(content4);

c++;

}

wbook.write(); //写入文件

wbook.close();

os.close();

} catch (Exception e) {

throw new Exception("导出文件出错");

}

}

}

class student {

private int ID;

private String name;

private String gender;

private String date;

public int getID() {

return ID;

}

public void setID(int iD) {

ID = iD;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getGender() {

return gender;

}

public void setGender(String gender) {

this.gender = gender;

}

public String getDate() {

return date;

}

public void setDate(String date) {

this.date = date;

}

public student()

{

super();

}

}

展开

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值