• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
爱白菜的小昆虫
博客园    首页    新随笔    联系   管理    订阅  订阅

java List 简单使用

Student类

class Student{
	String name;
	String pwd;
	public Student(){}
	public Student(String name, String pwd){
		this.name = name;
		this.pwd = pwd;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
}

TestList类

public class TestList {
	public static void main(String[] args) {
		List list = new ArrayList();//创建List对象
		for(int i = 0; i < 4; ++i){//向list对象中添加stu元素
			Student stu = new Student("小昆虫"+i, "100"+i);
			list.add(stu);
		}
		Iterator it = list.iterator();//使用迭代器访问list
		while(it.hasNext()){//判断迭代器是否在链表末尾
			Student stu = (Student) it.next();//访问当前元素
			System.out.println(stu.getName()+'\t'+stu.getPwd());//打印信息
		}
	}
}

运行效果

 

 

 

posted @ 2015-04-22 17:48  爱白菜的小昆虫  阅读(258)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3