//这个是StudentInfo类保存学生的信息
public class StudentInfo {
private int id ;
private String name;
private int age;
public StudentInfo(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class StudentUtil
{
FileReader fr=null;
BufferedReader br=null;
String _temp="";
List list=new ArrayList();
public void init(String filepath)
{
try
{
//创建一个FileReader的实例获取文件路径;
fr=new FileReader(filepath);
//用缓冲流包装fr实例;
br=new BufferedReader(fr);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
try
{
//判断输入文件是否到最后
while((_temp=br.readLine())!=null)
{
//将每一行的字段按照空格拆开并保存在_temp2的数组中
String _temp2[]=_temp.split(" ");
//进行强制转换
int no=Integer.parseInt(_temp2[0]);
String name=_temp2[1];
int age=Integer.parseInt(_temp2[2]);
//传回StudentInfo的构造函数里并放将对象放入list中
StudentInfo stu=new StudentInfo(no,name,age);
list.add(stu);
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
取出方式:
//用for循环取出对象
StudentInfo s=new StudentInfo();
for(int i=0;i
s=(StudentInfo)list.get(i); //注意向下转型
//取出StudentInfo对象中的参数
id=s.getId();
age=s.getAge();
name=s.getName();
}
第二:反射的问题,取出list 中所存对象的属性值(多个对象)
转自:http://topic.csdn.net/u/20080309/21/ea39337a-0164-4ff4-8c76-e8a64b5d7535.html
List list=new ArrayList();
Bean b1 = new Bean();
Bean b2 = new Bean();
list.add(b1);
list.add(b2);
b1和b2中都分别有两个属性 name 和 sex。要通过反射得到获取list中b1和b2的name和sex
取出方式:
//Bean.java
package com.sort;
public class Bean {
private String name;
private String sex;
public Bean() {
super();
// TODO Auto-generated constructor stub
}
public Bean(String name, String sex) {
super();
this.name = name;
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
//Test.java
package com.sort;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
Method metd = null;
String fdname = null;
// 添加两个测试数据。。。
List list = new ArrayList();
list.add(new Bean("张三", "女"));
list.add(new Bean("李四", "男"));
try {
// 遍历集合
for (Object object : list) {
Class clazz = object.getClass();// 获取集合中的对象类型
Field[] fds = clazz.getDeclaredFields();// 获取他的字段数组
for (Field field : fds) {// 遍历该数组
fdname = field.getName();// 得到字段名,
metd = clazz.getMethod("get" + change(fdname), null);// 根据字段名找到对应的get方法,null表示无参数
if ("name".equals(fdname) && metd != null) {// 比较是否在字段数组中存在name字段,如果不存在短路,如果存在继续判断该字段的get方法是否存在,同时存在继续执行
Object name = metd.invoke(object, null);// 调用该字段的get方法
System.out.print("姓名:" + name);// 输出结果
}
if ("sex".equals(fdname) && metd != null) {// 同上
Object sex = metd.invoke(object, null);
System.out.println("\t性别:" + sex);
}
}
}
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @param src
* 源字符串
* @return 字符串,将src的第一个字母转换为大写,src为空时返回null
*/
public static String change(String src) {
if (src != null) {
StringBuffer sb = new StringBuffer(src);
sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
return sb.toString();
} else {
return null;
}
}
}
黑色头发:http://heisetoufa.iteye.com/
分享到:
2009-04-03 00:39
浏览 25750
评论