Java-ArrayList遍历的实例-员工工资表
ArrayList遍历实例-员工工资表:
示例代码如下:
员工信息操作类
import com.sun.prism.impl.shape.BasicRoundRectRep;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class EmployeeOperator {
public static void main(String[] args) {
List arrayList = new ArrayList();
Employee zhangsan = new Employee(01,"张三","养猪部",2500.0);
Employee lisi =new Employee(02,"李四","掏粪部",3000);
Employee wangwu = new Employee(03,"王五","大数据开发",26849.8);
arrayList.add(zhangsan);
arrayList.add(lisi);
arrayList.add(wangwu);
//1,雇员工资打印:
System.out.println("本月员工名和工资: ");
for(int i=0; i<arrayList.size(); i++){
System.out.print(1+i+":"+ ((Employee)(arrayList.get(i))).getName());
System.out.println(" 实发工资: "+ ((Employee)(arrayList.get(i))).getSalary());
}
//2-1, 遍历工资单(for循环)
System.out.println("=====================");
for(int j=0; j<arrayList.size(); j++){
Employee s = (Employee)arrayList.get(j);
System.out.print(""+s.getId()+" ");
System.out.print(""+s.getName()+" ");
System.out.print(""+s.getDepartment()+" ");
System.out.println(""+s.getSalary());
}
///2-2 遍历工资单(迭代器)
System.out.println("=====================");
Iterator iterator = arrayList.iterator();
while(iterator.hasNext()){
Employee s = (Employee) iterator.next();
System.out.println(""+s.getId()+" "+s.getName() +" "+s.getDepartment()+" "+s.getSalary());
}
///2-3 遍历工资单(foreach循环)
System.out.println("=====================");
for (Object object: arrayList) {
Employee s =(Employee) object;
System.out.println(""+s.getId()+" "+s.getName() +" "+s.getDepartment()+" "+s.getSalary());
}
}
}
///员工信息类
public class Employee {
private int id;
private String name;
private String department;
private double salary;
public Employee(int id, String name, String department, double salary) {
this.id = id;
this.name = name;
this.department = department;
this.salary = salary;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
- 执行结果:


浙公网安备 33010602011771号