core-java学习【二】
时间:2010-07-18 来源:zuoqiang
package com;
import java.util.*;
public class ArrayListTest
{
public static void main(String[] args)
{
// fill the staff array list with three Employee objects
// 声明和构造一个保存Employee对象的数组列表
ArrayList<Employee> staff = new ArrayList<Employee> () ;
Employee temp1 = new Employee("Eric", "Zhang", 80000, 1989, 3, 14) ;
Employee temp2 = new Employee("Huiyi", "Chen", 80000, 1989, 3, 14) ;
staff.add(new Employee("Carl", "Cracker", 75000, 1987, 12, 15)) ;
staff.add(new Employee("Harry", "Hacker", 50000, 1989, 10, 1)) ;
staff.add(new Employee("Tony", "Tester", 40000, 1990, 3, 15)) ;
staff.add(temp2) ;
//使用add方法将对象添加到列表的指定位置,而不会覆盖原值,原有值会自动往下移动一格
staff.add(0 , temp1) ;
//使用set方法将对象添加到列表的指定位置,会覆盖原值
staff.set(4 , temp2) ;
// 使用remove方法将指定位置的对象从列表中删除,其后的对象将自动往上移一个同时调整列表大小
staff.remove(3) ;
// 方法 size()返回当前列表中元素的个数
System.out.println("\nstaff.size()= " + staff.size() + "\n");
// raise everyone's salary by 5%
for(Employee e : staff)
e.raiseSalary(5) ;
/* 等同于如下for循环
* for (int i = 0; i < staff.size(); i++)
* {
* Employee e = (Employee)staff.get(i) ;
* e.raiseSalary(5);
* }
*/
// print out the information about all Employee objects
for(Employee e : staff)
System.out.println("Name= " + e.getName()
+ " , salary= " + e.getSalary()
+" , hireDay= " + e.getHireDay()) ;
}
}
相关阅读 更多 +










