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 stubIterator 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集合基本方法演示

相关文章:

你感兴趣的文章:

标签云: