Java 集合 之 List 和 ArrayList

http://www.verejava.com/?id=1699409760261

package com.obj;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class TestList
{
	public static void main(String[] args)
	{
		List list=new ArrayList();
		//添加
		list.add(new Person("zhangshan",30));
		list.add(new Person("lishi",40));
		
		//删除
		
		//修改
		
		//遍历
		System.out.println("以索引方式遍历");
		for(int i=0;i<list.size();i++)
		{
			Person p=(Person)list.get(i);
			System.out.println(p.getName()+","+p.getAge());
		}
		System.out.println("以迭代器方式输出");
		Iterator iter=list.iterator();
		while(iter.hasNext())
		{
			Person p=(Person)iter.next();
			System.out.println(p.getName()+","+p.getAge());
		}
	}
}





package com.obj;

public class Person
{
	private String name;
	private int age;
	
	public Person(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;
	}
	
	
	
}

http://www.verejava.com/?id=1699409760261

posted @ 2018-06-24 07:38  verejava  阅读(111)  评论(0编辑  收藏  举报