泛型数组列表ArrayList

来源:互联网 发布:淘宝店铺类别 编辑:程序博客网 时间:2024/06/14 08:41

将Employee[]数组替换成ArrayList<Employee>,有以下变化

不必指出数组的大小

使用add将任意多的元素添加到数组中

使用size()替代length计算元素的数目

使用a.get(i)替代a[i]访问元素

ArrayList<>尖括号中的类型参数不允许是基本类型。即不允许写成ArrayList<int>,而只能用Integer对象包装器类ArrayList<Integer> list = new ArrayList<>();

ArrayList<>与数组之间的转换

1.数组转ArrayList--直接转
import java.util.ArrayList;public class Test{public static void main(String args[]){Employee[] staff = new Employee[2];staff[0] = new Employee("alice");staff[1] = new Employee("bob");ArrayList<Employee> list = new ArrayList<>();list.add(staff[0]);list.add(staff[1]);System.out.println(list.size());for(Employee e: list){System.out.println(e.getName());}}}class Employee{private String name;public Employee(String n){name = n;}public String getName(){return name;}}
output:
2alicebob
2. ArrayList转数组--toArray
public class Test{public static void main(String args[]){ArrayList<Employee> list = new ArrayList<>();list.add(new Employee("alice"));list.add(new Employee("bob"));System.out.println(list.size());Employee[] staff = new Employee[list.size()];list.toArray(staff);//把list放进staff数组中for(Employee e: staff){System.out.println(e.getName());}}}
output:
2alicebob

ArrayList<E>示例小程序

import java.util.*;/** * This program demonstrates the ArrayList class. */public class Test{public static void main(String args[]){ArrayList<Employee> staff = new ArrayList<>();staff.add(new Employee("Alice", 75000, 1987, 12, 15));staff.add(new Employee("Bob", 50000, 1989, 10, 1));staff.add(new Employee("Carl", 40000, 1990, 3, 15));for(Employee e: staff){e.raiseSalary(5);}for(Employee e: staff){System.out.println("name="+e.getName()+", salary="+e.getSalary()+", hireDay="+e.getHireDay());}}}class Employee  {      private String name;      private double salary;      private Date hireDay;            public Employee(String n, double s, int year, int month, int day)      {          name = n;          salary = s;          GregorianCalendar cal = new GregorianCalendar(year, month-1, day);          hireDay = cal.getTime();      }            public String getName()      {          return name;      }            public double getSalary()      {          return salary;      }            public Date getHireDay()      {          return hireDay;      }            public void raiseSalary(double percent)      {          salary *= (1 + percent/100);      }            public boolean equals(Object obj)      {          if(this == obj)              return true;          if(this == null)              return false;          if(this.getClass() != obj.getClass())              return false;                    Employee tmp = (Employee)obj;          return Objects.equals(name, tmp.name) && this.salary==tmp.salary && hireDay.equals(tmp.hireDay);      }            public int hashCode()      {          return Objects.hash(name, salary, hireDay);      }            public String toString()      {          return getClass().getName()+"[name="+name+",salary="+salary+",hireDay="+hireDay+"]";      }  }  
output:
name=Alice, salary=78750.0, hireDay=Tue Dec 15 00:00:00 CST 1987name=Bob, salary=52500.0, hireDay=Sun Oct 01 00:00:00 CST 1989name=Carl, salary=42000.0, hireDay=Thu Mar 15 00:00:00 CST 1990