1.泛型
2.集合框架
3.向上转型和向下转型
4.ArrayList学习
5.详细讲解集合元素迭代与LinkedList
6.栈(stack)
7.Queue接口及实现类ArrayBlockingQu
8.ArrayList版通讯录开发
9.讲解HashMap
10.讲解TreeMap与自然顺序Comparable接口
11.讲解TreeMap与第三方比较器Comparator
12.讲解HashSet和HashMap
1.泛型
没有类型限制的泛型:
/**
* 泛型类
* Created by yz on 2018/3/8.
*/
public class Cat<T> {
public void eat(T food){
System.out.println("猫吃:"+food);
}
public static void main(String[] args) {
Cat<String> cat = new Cat<String>();
cat.eat("鱼");
}
}
/**
* Created by yz on 2018/3/8.
*/
public class MyCat {
/**
* 泛型方法
* @param food
* @param <T>
*/
public <T> void eat (T food){
System.out.println("猫吃:"+food);
}
public static void main(String[] args) {
MyCat cat = new MyCat();
cat.eat("鱼");
}
}
有类型限制的泛型:
/**
* 警察泛型类 警察拿的武器一定是武器Weapon或武器的子类Gun
* Created by yz on 2018/3/8.
*/
public class Policeman <T extends Weapon>{
public void fankong(T wq){
System.out.println("警察手持"+wq+"反恐");
}
public static void main(String[] args) {
Policeman<Weapon> p1 = new Policeman<Weapon>();
p1.fankong(new Weapon());
Policeman<Gun> p2 = new Policeman<Gun>();
p2.fankong(new Gun());
/**
* Ak47 没有继承Weapon 所以这里报错
*/
/*Policeman<Ak47> p2 = new Policeman<Ak47>();
p2.fankong(new Ak47());*/
}
}
/**
* 警察
* Created by yz on 2018/3/8.
*/
public class Policeman {
/**
* 泛型方法 警察拿的武器一定是武器Weapon或武器的子类Gun
* @param wq
* @param <T>
*/
public <T extends Weapon> void fankong(T wq){
System.out.println("警察手持"+wq+"反恐");
}
public static void main(String[] args) {
Policeman p1 = new Policeman();
p1.fankong(new Weapon());
Policeman p2 = new Policeman();
p2.fankong(new Gun());
/**
* Ak47 没有继承Weapon 所以这里报错
*/
/*Policeman<Ak47> p2 = new Policeman<Ak47>();
p2.fankong(new Ak47());*/
}
}
----------------------------------------------------------------------------
public class MainTest { public static void main(String[] args) { Test<String> t = new Test<>("a"); t.setT("aaa");; t.test(); Test<Integer> t1 = new Test<>(1); t1.setT(111); t1.test(); Test<User> t2 = new Test<>(new User("xiaoming",18)); User u = t2.getT(); System.out.println(u); t2.test(); } } class Test<T> { private T t; public Test(T t) { this.t = t; } public T getT() { return t; } public void setT(T t) { this.t = t; } public void test() { System.out.println(t +" "+ t.getClass()); } } class User { String username; int age; public User(String username, int age) { this.username = username; this.age = age; } public String getUsername() { return username; } public int getAge() { return age; } @Override public String toString() { return "User{" + "username='" + username + '\'' + ", age=" + age + '}'; } }
----------------------------------------------------------------------------
2.集合框架
/**
* Interface List<E> - Class Vector<E> 底层数组:protected Object[] elementData;
* Created by yz on 2018/3/8.
*/
public class VectorDemo {
public static void main(String[] args) {
Vector v = new Vector();
System.out.println("当前容量:"+v.capacity());
System.out.println(v);
v.add("hello");
v.add(123);
v.add(true);
v.add(new double[]{1.3,4.5});
v.add('c');
System.out.println(v);
Object o = v.get(2); //里氏替换(向上转型)
boolean b = (Boolean)o; //向下转型
System.out.println(b); //true
v.set(0,"world");
System.out.println(v);
v.remove(1);
System.out.println(v);
}
}
3.向上转型和向下转型
/**
* 员工类
* Created by yz on 2018/3/8.
*/
public class Employee {
}
/**
* CEO CEO比普通员工多一个股份
* Created by yz on 2018/3/8.
*/
public class CEO extends Employee{
private int stock;
public int getStock() {
return stock;
}
public void setStock(int stock) {
this.stock = stock;
}
}
/**
* hr 管理员工
* Created by yz on 2018/3/8.
*/
public class HR {
private Employee[] emps = new Employee[3];
private int index = 0;
public void add(Employee e){
emps[index] = e;
index++;
}
public Employee getEmployee(int index){
return emps[index];
}
}
/**
* Created by yz on 2018/3/8.
*/
public class Persion {
@Override
public String toString() {
return "一个人";
}
/**
* 数据类型是身份、角色
* @param args
*/
public static void main(String[] args) {
Object p1 = new Persion(); // 从右往左看是向上转型[自动转],从左往右看是里氏替换 jvm给转的
System.out.println(p1);
Persion p2 = (Persion) p1; // 强制类型转换 向下转型
CEO c1 = new CEO();
HR hr = new HR();
hr.add(c1); // add的形参是Employee,实参是CEO,这里是向上转型
// 为什么要向上转型?为了统一管理
// 为什么要向下转型?为了区别对待 统一管理,区别对待
Employee e2 = hr.getEmployee(0);
CEO c2 = (CEO) e2;
c2.getStock();
}
}
4.ArrayList学习
/**
* Created by yz on 2018/3/8.
*/
public class ArrayListDemo {
public static void main(String[] args) {
// 接口是双方合作的协议 右边实现类,左边接口
List<String> list = new ArrayList<String>();
// 查看线性表中元素个数
list.add("123");
list.add("234");
list.add("345");
System.out.println(list);
list.remove("123");
System.out.println(list);
list.remove(1);
list.set(0,"111");
list.set(list.lastIndexOf("111"),"apple");
list.add(list.indexOf("apple"),"苹果");
System.out.println(list);
//从列表中删除所有元素
//list.clear();
// 查看集合是否包含指定的元素
boolean b = list.contains("123");
System.out.println(b);
// 查看集合是否为空
b = list.isEmpty();
System.out.println(b);
// 集合变数组
Object[] objs = list.toArray();
System.out.println(objs.length);
List<String> list2 = new ArrayList<String>();
list2.add("头");
list2.add("尾");
list2.addAll(1,list);
System.out.println(list2);
}
}