集合类都在java.util包下。Java的集合框架提供了操作一组数据的很多方法,这些方法我们直接调用,不用自己去实现。利用java类库帮我们在程序设计中完成数据结构课程中的各种对象。
Collections框架中,主要有4个接口:Collection 表示集合、Set 不允许重复的集合、List可以有重复元素的集合、Map 键-值 映射对
集合的特点:用于存储对象的容器(存储对象的引用),集合的长度是可变的,集合中不可以存储基本数据类型值
集合与数组的区别: 集合中可以存储任意的对象,且长度是可变的,数组中只能存储同一类型的数据,且长度是不可变的。
Collection:
//Person类
class Person
{
private String name;
private String sex;
private int age;
public Person(String name, String sex, int age)
{
this.name = name;
this.sex = sex;
this.age = age;
}
public String getName()
{
return name;
}
public String getSex()
{
return sex;
}
public int getAge()
{
return age;
}
//Employee继承Person类
class Employee extends Person
{
private String id;
private String department;
public Employee(String name, String sex, int age, String id, String department)
{
super(name, sex, age);
this.id = id;
this.department = department;
}
public String getId()
{
return id;
}
}
//Customer类继承Person
class Customer extends Person
{
private String address;
private String phone;
public Customer(String name, String sex, int age, String address, String phone)
{
super(name, sex, age);
this.address = address;
this.phone = phone;
}
public String getAddress()
{
return address;
}
public String getPhone()
{
return phone;
}
}
//EmployeeArray类
class EmployeeArray
{
private Employee[] personList;
public EmployeeArray(int size)
{
personList = new Employee[size];
}
public boolean Insert(int index, Employee e)
{
if(index >= 0 && index < personList.length)
{
personList[index] = e;
return true;
}
return false;
}
public Employee get(int index)
{
if(index >= 0 && index < personList.length)
{
return personList[index];
}
return null;
}
}
//CustomerArray类
class CustomerArray
{
private Customer[] personList;
public CustomerArray(int size)
{
personList = new Customer[size];
}
public boolean Insert(int index, Customer e)
{
if(index >= 0 && index < personList.length)
{
personList[index] = e;
return true;
}
return false;
}
public Customer get(int index)
{
if(index >= 0 && index < personList.length)
{
return personList[index];
}
return null;
}
//主方法
public class Test
{
public static void main(String[] args)
{
EmployeeArray ea = new EmployeeArray(5);
CustomerArray ca = new CustomerArray(5);
Employee e1 = new Employee("王晓东","男", 25, "0001", "生产部");
Employee e2 = new Employee("王文欢","男", 26, "0002", "营销部");
Employee e3 = new Employee("吴邱润","男", 27, "0003", "生产部");
Employee e4 = new Employee("刘占力","男", 30, "0004", "生产部");
Employee e5 = new Employee("刘冬雪","女", 24, "0005", "营销部");
ea.Insert(0, e1);
ea.Insert(1, e2);
ea.Insert(2, e3);
ea.Insert(3, e4);
ea.Insert(4, e5);
Employee e = ea.get(2);
System.out.println(e.getName());
Customer c1 = new Customer("杨巍", "女", 21, "沈阳市和平区", "11111111");
Customer c2 = new Customer("李军", "男", 30, "沈阳市大东区", "22222222");
Customer c3 = new Customer("孙浩", "男", 25, "沈阳市皇姑区", "33333333");
ca.Insert(0, c1);
ca.Insert(1, c2);
ca.Insert(2, c3);
Customer c = ca.get(2);
System.out.println(c.getName());
}
}
}
EmployeeArray类和CustomerArray类的代码差别只是保存对象的类型不同,其它的全部相同。
解决方案: 因为Employee和Customer都继承于Person,所以可以将容器类设计成保存Person类型对象的类,这样,只需要一个类就可以保存多种类型的对象了。
第一种解决方案:
//类PersonArray
class PersonArray
{
private Person[] personList;
public PersonArray(int size)
{
personList = new Person[size];
}
public boolean Insert(int index, Person e)
{
if(index >= 0 && index < personList.length)
{
personList[index] = e;
return true;
}
return false;
}
public Person get(int index)
{
if(index >= 0 && index < personList.length)
{
return personList[index];
}
return null;
}
}
第二种解决方案:将PersonArray类改为泛型方式实现
class PersonArray<T>
{
private T[] personList;
public PersonArray(T[] personList)
{
this.personList = personList;
}
public boolean Insert(int index, T e)
{
if(index >= 0 && index < personList.length)
{
personList[index] = e;
return true;
}
return false;
}
public T get(int index)
{
if(index >= 0 && index < personList.length)
{
return personList[index];
}
return null;
}
}
//main
public class Test
{
public static void main(String[] args)
{
Employee[] elist = new Employee[5];
PersonArray<Employee> ea = new PersonArray<Employee>(elist);
Customer[] clist = new Customer[5];
PersonArray<Customer> ca = new PersonArray<Customer>(clist);
Employee e1 = new Employee("王晓东","男", 25, "0001", "生产部");
Employee e2 = new Employee("王文欢","男", 26, "0002", "营销部");
Employee e3 = new Employee("吴邱润","男", 27, "0003", "生产部");
Employee e4 = new Employee("刘占力","男", 30, "0004", "生产部");
Employee e5 = new Employee("刘冬雪","女", 24, "0005", "营销部");
ea.Insert(0, e1);
ea.Insert(1, e2);
ea.Insert(2, e3);
ea.Insert(3, e4);
ea.Insert(4, e5);
Employee e = ea.get(2);
System.out.println(e.getName());
Customer c1 = new Customer("杨巍", "女", 21, "沈阳市和平区", "11111111");
Customer c2 = new Customer("李军", "男", 30, "沈阳市大东区", "22222222");
Customer c3 = new Customer("孙浩", "男", 25, "沈阳市皇姑区", "33333333");
ca.Insert(0, c1);
ca.Insert(1, c2);
ca.Insert(2, c3);
Customer c = ca.get(2);
System.out.println(c.getName());
}
}