public class CollectionDemo4 {
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void main(String[] args) {
// 创建User对象
User u1 = new User("关羽", 18);
User u2 = new User("赵云", 28);
User u3 = new User("张飞", 38);
User u4 = new User("吕布", 8);
User u5 = new User("貂蝉", 16);
//创建集合
Collection c = new ArrayList();
c.add(u1);
c.add(u2);
c.add(u3);
c.add(u4);
c.add(u5);
//遍历
//转换
Object[] array = c.toArray();
for (int i = 0; i < array.length; i++) {
User u = (User)array[i]; // 强制转换为User
System.out.println(u.getName() +" ---- " + u.getAge());
// 你操作 user ,年龄+10
int now_age = u.getAge();
public class User {
String name ;
int age;
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}