一.泛型
1.泛型:标识集合中存储元素的数据类型
写法:<数据类型>
2.泛型的好处:
1).保证数据安全
2)避免向下转型
3)将运行时报错转化到编译时报错
public class Day14 {
public static void main(String[] args) {
//创建集合
//注意:前后泛型的类型要一致,后面的泛型的类型可以不写,系统默认前后的类型一致
ArrayList<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
//迭代器泛型表示的是集合中保存的元素的类型
Iterator<String> iterator = list.iterator();
while(iterator.hasNext()){
//使用泛型不用类型的强转
String str = iterator.next();
System.out.println(str);//输出:a,b,c
}
//将运行时的报错转化到编译时的报错
ArrayList<Student> list1 = new ArrayList<>();
list1.add(new Student("张三", 18));
list1.add(new Student("李四", 16));
list1.add(new Student("王五", 20));
Student student = list1.get(0);
System.out.println(student.getName());
//创建泛型类的对象
Worker<String> worker = new Worker<>();
worker.setL("张三");//张三
System.out.println(worker.getL());
worker.fn("团结就是力量");//团结就是力量
Worker.fn2("哈哈");//哈哈
}
}
3.多参数方法int …num
调用方式:
1).传入多个值以逗号隔开
2).传入数组
注意:多参数时要放在参数列表的前面
public class Day14 {
public static void main(String[] args) {
//传入多个值调用
print(1, 2, 3);
//传入数组调用
int[] array = {1, 2, 3};
print(array);
}
public static void print(int ... num){
//遍历
for (int i = 0; i < num.length; i++) {
System.out.println(num[i]);
}
}
}
二.数组转集合
public class Day14 {
public static void main(String[] args) {
int[] array = {1, 2, 3, 4};
//把数组当做一个参数放入集合没有自动装箱
List list = Arrays.asList(array);
System.out.println(list);//{1, 2, 3, 4}
//系统进行自动装箱
Integer[] array2 = {1, 2, 3, 4};
List list1 = Arrays.asList(array2);
System.out.println(list1);//输出:[1, 2, 3, 4]
//创建一个字符串数组 保存三个名字,将数组 转成集合并添加一个名字
String[] str = {"张三", "李四", 王五};
List<String> list2 = Arrays.asList(str);
//使用asList()方法转集合后,该集合不能进行长度的修改,但可以使用集合的其他方法
//list2.add("小刘");//报错
boolean b = list2.contains("小王");
System.out.println(b);//false
2.泛型? extends E
子类 继承 父类 传入的参数必须是父类的子类或本类,可以限定参数的类型
ArrayList<Person> list3 = new ArrayList<>();
list3.add(new Person("张三", 18));
list3.add(new Person("李四", 20));
ArrayList<Student> list4 = new ArrayList<>();
list4.add(new Student("ww", 18));
list4.add(new Student("xl", 20));
//? list4 的泛型 E list3的泛型
list.addAll(list1);
System.out.println(list);
}
}
三.集合中删除的两种方法
public class Day14 {
public static void main(String[] args) {
1.迭代器删除(删除元素b)
ArrayList<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("b");
list.add("c");
list.add("d");
Iterator<String> iterator = list.iterator();
while(iterator.hasNext()){
String str = iterator.next();
if(str.equals("b")){
//使用迭代器的方法删除
iterator.remove();
}
}
ystem.out.println(list);//a, c, d
2.for循环删除
for(int i= 0; i< list.size(); i++){
if(list.get(i).equals("b")){
list.remove(i);
i--;
}
}
System.out.println(list);//a, c, d
}
}
2.Collections的排序方法
创建集合,保存三个学生按年龄降序 排序
public class Day14 {
public static void main(String[] args) {
ArrayList<Student> list = new ArrayList<>();
list.add(new Student("张三", 18));
list.add(new Student("李四", 16));
list.add(new Student("王五", 20));
list.add(new Student("小刘", 23));
list.add(new Student("小伟", 25));
//使用双重循环
for(int i = 0; i < list.size() - 1; i++){
for(int j = 0; j < list.size() - 1 - i; j++){
Collection.swap(list, j , j + 1);
}
}
System.out.println(list);
}
}
四.HashSet
1.Set集合是无下标,无序,不重复的
2.hashSet去重
//创建set集合添加a a b b c c 迭代器遍历
public class Day14 {
public static void main(String[] args) {
HashSet set = new HashSet();
//集合中的有序是指存取得顺序
hashSet.add("t");
hashSet.add("t");
hashSet.add("a");
hashSet.add("a");
hashSet.add("b");
hashSet.add("b");
hashSet.add("c");
hashSet.add("c");
//获取迭代器
Iterator<String> iterator = set.iterator();
//循环遍历
while (iterator.hasNext()) {
String next = iterator.next();
System.out.println(next);//输出:t, a, b, c
}
//创建六个人两两重复,使用set去重
//系统每创建一个对象都会分配一个hashCode值当向hashSet集合中保存对象时,系统会先比较hashCode值是否相同,相同时再调用equals方法进行比较不同时存入反之不存.而hashCode值不同时直接存入集合中,不会调用equals方法比较
hashSet.add(new Man("李四", 18));
hashSet.add(new Man("李四", 18));
hashSet.add(new Man("张三", 16));
hashSet.add(new Man("张三", 16));
hashSet.add(new Man("王五", 21));
hashSet.add(new Man("王五", 21));
Iterator<Man> iterator = hashSet.iterator();
while (iterator.hasNext()) {
Man next = iterator.next();
System.out.println(next);
}
//随机十个数[10,20]放集合中去重
String和Integer可以直接去重,系统已经重新写好了hashCode和equals方法
HashSet<Integer> hashSet = new HashSet<>();
for (int i = 0; i < 10; i++) {
int num = (int) (Math.random() *(20 - 10 + 1) + 10);
hashSet.add(num);//添加时系统自动装箱,添加时已去重
}
System.out.println(hashSet);
}
}
//Person类
public class Person {
private String name;
private int age;
public Person() {
super();
// TODO Auto-generated constructor stub
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
//学生类
public class Student extends Person {
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(String name, int age) {
super(name, age);
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
// TODO Auto-generated method stub
return super.toString();
}
}
//工人类
public class Worker<L>{
//利用泛型声明成员变量
private L l;
//声明set/get方法
public void setL(L l) {
this.l = l;
}
public L getL() {
return this.l;
}
public void fn(L l) {
System.out.println(l);
System.out.println("我是fn方法");
}
//一个类可以有多个泛型
//需要在方法上进行泛型的声明,这个泛型会在该方法调用的时候被赋值
public<Y> void fn1(Y y) {
System.out.println(y);
}
//静态方法不能使用W泛型
//当方法被调用时方法被赋值,类的加载
public static<Q> void fn2(Q q) {
System.out.println(q);
}
}
// Man类
public class Man {
private String name;
private int age;
public Man() {
super();
// TODO Auto-generated constructor stub
}
public Man(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
//toString方法的重写
@Override
public String toString() {
return "Man [name=" + name + ", age=" + age + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
equals方法的重写
@Override
public boolean equals(Object obj) {
if (this == obj)//比较地址
return true;
if (obj == null)//一个为空
return false;
if (getClass() != obj.getClass())//判断两个对象是否来自一个类
return false;
Man other = (Man) obj;//向下转型
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))//equals调用String对象的方法
return false;
return true;
}
}