1、创建ArrayList对象,
2、显示对象的尺寸
3、添加具体员工到Arraylist容器中
4、显示员工的姓名
------解决方案--------------------
package com.niit.collection;
/**
*user类
* @author Administrator
*
*/
public class User
{
private int userId;
private String userName;
public User(int userId,String userName)
{
this.userId=userId;
this.userName=userName;
}
public int getUserId()
{
return userId;
}
public void setUserId(int userId)
{
this.userId = userId;
}
public String getUserName()
{
return userName;
}
public void setUserName(String userName)
{
this.userName = userName;
}
}
package com.niit.collection;
import java.util.ArrayList;
public class Exam1
{
public static void main(String[] args)
{
//创建arraylist
ArrayList<User> list=new ArrayList<User>();
//显示对象的尺寸--不懂什么意思,难道是list的长度?
//System.out.println(list.size());
//添加员工
list.add(new User(1, "tom"));
list.add(new User(2, "tom1"));
list.add(new User(3, "tom2"));
list.add(new User(4, "tom3"));
list.add(new User(5, "tom4"));
//显示员工姓名
for(User user : list)
{
System.out.println(user.getUserName());
}
}
}