(筆記)關於集合中ArrayList例子
阿新 • • 發佈:2019-02-12
集合:
主要是分成以下幾種
List結構集合類
ArrayList LinedList Vector Stack
Map結構集合類
HashMap Hashtable
Set結構的集合類
HashSet TreeSet
Queue結構的集合
Queue介面
ArrayList類:實現了可變陣列,允許儲存所有元素,包括null並可以根據索引位置對集合進行快速訪問和隨機訪問
缺點:向指引的索引位置插入物件或刪除物件比較慢
例項:
ArrayList可以新增重複的物件。package lei; import java.util.*; import java.io.*; public class newDemo { public static void main(String[] args) throws Exception{ // TODO Auto-generated method stub EmpManage em=new EmpManage(); BufferedReader br =new BufferedReader(new InputStreamReader(System.in));//br流 while(true){ System.out.println("選擇要進行的操作"); System.out.println("1:新增員工"); System.out.println("2:查詢員工"); System.out.println("3:修改薪水"); System.out.println("4:刪除員工"); System.out.println("5:顯示所有員工"); String operType=br.readLine(); if(operType.equals("1")){ //判斷是否相等 System.out.println("請輸入編號"); String empno=br.readLine(); System.out.println("請出入名字"); String name=br.readLine(); System.out.println("請輸入薪水"); float sal =Float.parseFloat(br.readLine());//String強制轉換為float Emp emp=new Emp(empno,name,sal); em.addEmp(emp); }else if(operType.equals("2")){ System.out.println("請輸入編號"); String empno=br.readLine(); em.showinfo(empno); }else if(operType.equals("3")){ System.out.println("請輸入要修改的編號"); String empno=br.readLine(); System.out.println("請輸入修改後的薪水"); float newsal=Float.parseFloat(br.readLine()); em.updateSal(empno, newsal); }else if(operType.equals("4")){ System.out.println("請輸入編號"); String empno= br.readLine(); em.delemp(empno); }else if(operType.equals("5")){ em.showall(); } } } } class EmpManage{ private ArrayList al=null; //定義一個ArrayList public EmpManage(){ al=new ArrayList(); //賦值ArrayList } public void addEmp(Emp emp){ //方法1 al.add(emp); //集合中新增元素的方法 } public void showinfo(String empno){ for(int i=0; i<al.size();i++){ //遍歷集合元素 Emp emp = (Emp)al.get(i); //提取集合元素 if (emp.getEmpno().equals(empno)){ //判斷是否相等 System.out.println("找到該員工資訊是"); System.out.println("編號="+ empno); System.out.println("名字="+ emp.getName()); } } } public void updateSal(String empno,float newsal){ for(int i=0;i<al.size();i++){ //遍歷集合元素 Emp emp =(Emp)al.get(i); if(emp.getEmpno().equals(empno)){ emp.setSal(newsal); //set方法更新(覆蓋屬性) } } } public void delemp(String empno){ for(int i=0;i<al.size();i++){ Emp emp=(Emp)al.get(i); if(emp.getEmpno().equals(empno)){ al.remove(i); //集合中刪除元素 } } } public void showall(){ for(int i=0;i<al.size();i++){ Emp emp=(Emp)al.get(i); System.out.println("第"+i+"員工的基本資訊為:"); System.out.println("名字為"+emp.getName()); System.out.println("編號為"+emp.getEmpno()); System.out.println("薪水為"+emp.getSal()); } } } class Emp{ public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmpno() { return Empno; } public void setEmpne(String empne) { Empno = empne; } public float getSal() { return sal; } public void setSal(float sal) { this.sal = sal; } private String name; private String Empno; private float sal; public Emp(String name , String Empno,float sal){ this.name=name; this.sal=sal; this.Empno=Empno; } }