泛型陣列列表ArrayList

Pop_Rain發表於2017-06-29

將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:
2
alice
bob
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:
2
alice
bob

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 1987
name=Bob, salary=52500.0, hireDay=Sun Oct 01 00:00:00 CST 1989
name=Carl, salary=42000.0, hireDay=Thu Mar 15 00:00:00 CST 1990

相關文章