码迷,mamicode.com
首页 > 编程语言 > 详细

黑马程序员——java基础 ArrayList集合基本方法演示

时间:2015-06-19 10:37:59      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:黑马程序员   arraylist   list集合   java基础   java   




java基础 ArrayList集合基本方法演示




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


public class ArrayListDemos 
{

	public static void main(String[] args) 
	{
		// 创建一个新的容器
		ArrayList al = new ArrayList();
		al.add("abc1");
		al.add("abc2");
		al.add("abc3");
		al.add("abc4");
		al.add("abc5");
		printListMethod(al);
		System.out.println("-----------------------------------------------");
		//---------------------------------------------------
        al.removeAll(al);
        al.add(new Student("lisi",22));
        al.add(new Student("wangwu",25));
        al.add(new Student("kkk",26));
        al.add(new Student("wangwu",25));
        Iterator<Student>  it = al.iterator();
        while(it.hasNext())
        {
        	Student s = it.next();
        	System.out.println("the student name is :"+s.getName()+"\tand the age is :"+s.getAge());
        }    
	}

	private static void printListMethod(ArrayList al) 
	{
		// TODO Auto-generated method stub
		Iterator it = al.iterator();
		while(it.hasNext())
		{
			System.out.println(it.next());
		}
	}

}
class Student
{
	private String name;
	private int age;
	Student(String name ,int age)
	{
		this.name=name;
		this.age = age;
	}
	public int getAge()
	{
		return age;
		
	}
	public String getName()
	{
		return name;
		
	}
	public String toString()
	{
		return "the student name : +name "+"  and  age is : "+ age;
		
	}
	}




运行程序:


技术分享





黑马程序员——java基础 ArrayList集合基本方法演示

标签:黑马程序员   arraylist   list集合   java基础   java   

原文地址:http://blog.csdn.net/zl18603543572/article/details/46554891

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!