java 怎样处理对一个类创建多个对象
我编写了一下的程序想将多个对象导到excel中但是导出成功了,不过值都变成最后一条的数据通过debug我发现我创建的多个student对象其实只有一个而且对于插入到lis...
我编写了一下的程序 想将多个对象导到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();
}
} 展开
代码:
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();
}
} 展开
3个回答
展开全部
student stu = new student();
student stu1 = new student();
student stu2 = new student();
student stu3 = new student();
student stu4 = new student();
List list = new ArrayList(); //这个是从数据库中取得要导出的数据
stu1.setID(10);
stu1.setName("小丁");
stu1.setGender("1");
stu1.setDate("2012.12.12");
list.add(stu1);
明白了吗
student stu1 = new student();
student stu2 = new student();
student stu3 = new student();
student stu4 = new student();
List list = new ArrayList(); //这个是从数据库中取得要导出的数据
stu1.setID(10);
stu1.setName("小丁");
stu1.setGender("1");
stu1.setDate("2012.12.12");
list.add(stu1);
明白了吗
展开全部
for(int i = 0;i<list.size();i++){
Student stu = (Student)list.get(i);
stu.属性
}
或者lz把 stu = (student) it.next(); 改成Student stu = (Student)it.next();
Student stu = (Student)list.get(i);
stu.属性
}
或者lz把 stu = (student) it.next(); 改成Student stu = (Student)it.next();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
头晕了,没看懂你的方法,建议别做的那么复杂,套个循环,让STUDENT自动增长。或者楼主在换个思路。你这个我看了觉得程序做出来,冗YU太多了,不完美~
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询