泛型数组列表ArrayList使用实例(运行时自动调整数组容量)

本文介绍Java泛型概念及ArrayList泛型类的应用,通过创建Employee类存储到ArrayList中,并实现员工信息的增加薪资和展示等功能,最后展示了ArrayList转换为数组的方法。
泛型数组列表ArrayList:

ArrayList是一个采用类型参数的泛型类,<>中指定数组保存的元素的对象类型

实例讲解:

创建一个员工类Employee,用于存放到泛型数组中,也就是<>中放的就是Employee,然后main()方法中实现存储就可以了。

1.创建Employee类,这里没什么好讲的,你应该能看懂
import java.time.LocalDate;

public class Employee {

    private String name;
    private double salary;
    private LocalDate hireDay;

    public Employee(String mName, double mSalary, int year, int month, int day) {
        this.name = mName;
        this.salary = mSalary;
        this.hireDay = LocalDate.of(year, month, day);
    }

    public String getName() {
        return name;
    }

    public double getSalary() {
        return salary;
    }

    public LocalDate getHireDay() {
        return hireDay;
    }

    public void raiseSalary(double byPercent) {
        double raiseValue = salary * byPercent / 100;
        salary += raiseValue;
    }

}
2.创建ArrayListTest类在main()方法中实现存贮,展现的功能
import java.util.ArrayList;

public class ArrayListTest {

    public static void main(String[] args) {

        //fill the employeeList with the three Employee objects
        ArrayList<Employee> employeeList = new ArrayList<>();

        employeeList.add(new Employee("Jack", 10000, 2018, 1, 1));
        employeeList.add(new Employee("Rose", 20000, 2018, 1, 1));
        employeeList.add(new Employee("Mike", 30000, 2018, 1, 1));

        //raise employee salary by 10%
        for (Employee e : employeeList) {
            e.raiseSalary(10);
        }
        //print out the information about the three employee objects
        for (Employee ee : employeeList) {
            System.out.println(
                    "name=" + ee.getName() + "  " + "salary=" + ee.getSalary() + "  " + "hireDay=" + ee.getHireDay());
        }
    }
}
现在我们的实例就结束了,这里我添加一个ArrayList转换成数组的简单方法
import java.util.ArrayList;

public class ArrayListTest {

    public static void main(String[] args) {

        .........
        //print out the information about the three employee objects
        for (Employee ee : employeeList) {
            System.out.println(
                    "name=" + ee.getName() + "  " + "salary=" + ee.getSalary() + "  " + "hireDay=" + ee.getHireDay());
        }

        //使用toArray()方法可以很轻松的实现ArrayList转为数组
        Employee[] array = new Employee[employeeList.size()];
        employeeList.toArray(array);

        System.out.println();

        for (Employee eee : array) 
        {

            System.out.println("name=" + eee.getName() + "  " + "salary=" + eee.getSalary() + "  " + "hireDay="
                    + eee.getHireDay());
        }

    }

}
效果截图:

这里写图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

intoSunshine

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值