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




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;
		
	}
	}




运行程序:







早起的年轻人 CSDN认证博客专家 移动开发 项目管理 Java
只要用心去做,每一件事情还是有可能成功的,当然成功是没有界限的,只不过是达到自己心里的那个目标,公众号:我的大前端生涯,一个爱喝茶的程序员,通常会搞搞SpringBoot 、Herbinate、Mybatiys、Android、iOS、Flutter、Vue、小程序等.
©️2020 CSDN 皮肤主题: 代码科技 设计师:Amelia_0503 返回首页