标签:黑马程序员 arraylist list集合 java基础 java
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