泛型数组列表ArrayList的实例

ArrayList是一个采用类型参数(type parameter)的泛型类(generic class)。为了指定数组列表保存的元素对象类型,需要用一对尖括号将类名括起来加在后面,例如,

 ArrayList<Employee> staff = new ArrayList<Employee>();
 ArrayList<Employee> staff = new ArrayList<>();
 ArrayList<Employee> staff = new ArrayList<Employee>(size);
 ArrayList<Employee> staff = new ArrayList<>(size);


这被称为“菱形”语法。

数组列表管理着对象引用的望尘不及内部数组。最终,数组的全部空间有可能被用尽。这就显现出数组列表的操作魅力:如果调用add且内部数组已经满了,数组列表就就将自动创建一个更大的数组,并将所有的对象从较小的数组中拷贝到较大的数组中。

ArrayList部分源码如下:

 /**
     * Appends the specified element to the end of this list.
     *
     * @param e element to be appended to this list
     * @return <tt>true</tt> (as specified by {@link Collection#add})
     */
    public boolean add(E e) {
        ensureCapacityInternal(size + 1);  // Increments modCount!!
        elementData[size++] = e;
        return true;
    }
 
 private void ensureCapacityInternal(int minCapacity) {
        if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
            minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
        }
        ensureExplicitCapacity(minCapacity);
    }
    private void ensureExplicitCapacity(int minCapacity) {
        modCount++;
        // overflow-conscious code
        if (minCapacity - elementData.length > 0)
            grow(minCapacity);
    }
 
 /**
     * Increases the capacity to ensure that it can hold at least the
     * number of elements specified by the minimum capacity argument.
     *
     * @param minCapacity the desired minimum capacity
     */
    private void grow(int minCapacity) {
        // overflow-conscious code
        int oldCapacity = elementData.length;
        int newCapacity = oldCapacity + (oldCapacity >> 1);
        if (newCapacity - minCapacity < 0)
            newCapacity = minCapacity;
        if (newCapacity - MAX_ARRAY_SIZE > 0)
            newCapacity = hugeCapacity(minCapacity);
        // minCapacity is usually close to size, so this is a win:
        elementData = Arrays.copyOf(elementData, newCapacity);
    }

从grow()函数可以看出来ArrayList扩容机制的大小增长规则: 扩容大小相当于原来的1.5倍然后加1,

int newCapacity = oldCapacity + (oldCapacity >> 1);


很遗憾,天下没有免费的午餐。数组列表自动扩展容量的便利增加了访问元素语法的复杂程度。其原因是ArrayList类并不是Java程序设计语言的一部分;它只是某些人编写且被放在标准库中的一个实用类。

注:
  没有泛型类时,原始的ArrayList类提供的get方法别无选择只能返回Object,此外,get方法的调用者必须对返回值进行类型转换:
 

Employee e = (Employee)staff.get(i);

原始的ArrayList存在着一定的危险性。它的add和set方法允许接受任意类型的对象。对于下面这个调用
 

staff.set(i, new Date());

编译不会给出任何警告。只有在检索对象并试图对它进行类型转换时,才会发现有问题。如果使用ArrayList<Employee>,编译器就会检测到这个错误。

下面来看ArrayList一个实例:

import java.util.ArrayList;

public class ArrayListTest
{
	public static void main(String[] args)
	{
		ArrayList<Employee> staff = new ArrayList<>();
		staff.add(new Employee("Carl Craker", 7500, 1987, 12, 15));
		staff.add(new Employee("Tom", 80000, 1988, 3, 15));
		
		//raise everyone's salary by 5%
		for(Employee e : staff)
			e.raiseSalary(5);
		
		//print out information about all Empoyee objects
		for(Employee e : staff)
			System.out.println("naem=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay=" + e.getHireDay());
	}
}
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Objects;

public 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 calendar = new GregorianCalendar(year, month - 1, day);
		hireDay = calendar.getTime();
	}
	
	public String getName()
	{
		return name;
	}
	
	public double getSalary()
	{
		return salary;
	}
	
	public Date getHireDay()
	{
		return hireDay;
	}
	
	public void raiseSalary(double byPercent)
	{
		double raise = salary * byPercent / 100;
		salary += raise;
	}
	
	public int hashCode()
	{
		return Objects.hash(name, salary, hireDay);
	}
	
	public String toString()
	{
		return getClass().getName() + "[name=" + name + ",salary=" + salary + ",hireDay=" + hireDay + "]";
	}
	
	public boolean equals(Object otherObject)
	{
		//a quick test to see if the objects are identical
		if(this == otherObject)
			return true;
		
		//must return false if the explicit parameter is null
		if(otherObject == null)
			return false;
		
		//if classes don't match, they can't be equal
		if(getClass() != otherObject.getClass())
			return false;
		
		//now we know otherObject is a non-null Employee
		Employee other = (Employee)otherObject;
		
		//test whether the fields have identical values
		return Objects.equals(name, other.name) && salary == other.salary && Objects.equals(hireDay, other.hireDay);
	}
}

编译并运行程序,见图1。

                                                                                                 图1
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值