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;
}
}
- 执行结果:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)