List 练习Person 集合去重

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Person {
    private String name;
    private int age;

    @Override
    public boolean equals(Object obj) {
        Person p = (Person) obj;
        boolean d = false;
        if(this.getAge() == p.getAge() && this.getName() == p.getName()){
            d = true;
        } else{
            d = false;
        }
        return d;
    }
}
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Demo2_ArrayList {
    public static void main(String[] args) {
        List list = new ArrayList();
        list.add(new Person("小明", 23));
        list.add(new Person("小明", 23));
        list.add(new Person("小红", 22));
        list.add(new Person("小王", 25));

        List list2 = new ArrayList();
        deleteMu(list, list2);
        System.out.println(list2);

    }
    public static void deleteMu(List srcList, List destList){
        Iterator iTsrc = srcList.iterator();
        while (iTsrc.hasNext()){
            Person p = (Person) iTsrc.next();
            if( !destList.contains(p) ){

                destList.add(p);
            }
        }
        return ;
    }
}

LinkedList 方法

  • void addFirst(E e) void addLast(E e) 在头增加 或 在尾增加
  • public E getFirst() public E getLast() 获取第1个 或 最后1个
  • public E removeFirst() public E removeLast() 删除第1个或最后1个
  • public E get(int index) 获取指定位置的元素
import java.util.LinkedList;

public class Demo1_LinkedList {
    public static void main(String[] args) {
        LinkedList ll = new LinkedList();
        ll.add("a");
        ll.add("b");
        ll.add("c");
        ll.add("d");

        ll.addFirst(0);
        ll.addLast(10);
        System.out.println(ll);

        System.out.println(ll.getFirst());
        System.out.println(ll.getLast());

        ll.removeFirst();
        System.out.println(ll);
        ll.removeLast();
        System.out.println(ll);

        System.out.println(ll.get(0));
    }
}

LinkedList 模拟栈

import lombok.Data;

import java.util.LinkedList;

@Data
public class Stank {
    private LinkedList ll = new LinkedList();

    public  void in(Object obj){
        ll.addLast(obj);
    }
    public void out(){
        ll.removeLast();
    }
    public void outAll(){
        while (! ll.isEmpty()){
            ll.removeLast();
        }
    }
    public boolean isEmpty(){
        if(ll.isEmpty()){
            return true;
        }
        return false;
    }
}


import com.lizicai.bean.Stank;

public class Demo3_LinkedList {
    public static void main(String[] args) {
        Stank s = new Stank();
        s.in("a");
        s.in("b");
        s.in("c");
        s.out();
        System.out.println(s.getLl());
        s.outAll();
        System.out.println(s.getLl());
    }
}

泛型 generic

泛型的好处

  • 提高安全性, 把运行的错误转移到编译时校验
  • 省去强转的麻烦

前后泛型一致, 或者后面泛型是前端泛型的子类

public class Demo1_Generic {
    public static void main(String[] args) {
        ArrayList <Person> list = new ArrayList<>();
        list.add(new Person("小红", 23));
        list.add(new Person("小明", 23));

        ListIterator <Person> it = list.listIterator();
        while ( it.hasNext()){
            Person p = it.next();
            System.out.println(p.getName() + p.getAge());
        }
    }
}

类的泛型

  • 在类首行指定处理的引用数据类型
public class SePersion <E>{
    private E e;

    public E getP() {
        return e;
    }

    public void setP(E p) {
        this.e = p;
    }
}


import com.lizicai.bean.Person;
import com.lizicai.bean.SePersion;

public class Demo3_Generic {

    public static void main(String[] args) {
        SePersion<Person> s = new SePersion();
//        s.setP("kdfj");
        s.setP(new Person("小明", 23));
        System.out.println(s.getP());

        SePersion d = new SePersion();
        d.setP("dk");
        d.setP(new Person());
        System.out.println(d.getP());
    }
}

方法泛型

  • 方法泛型和类的泛型一至
  • 也可以不一致, 在使用方法时指定泛型
  • 静态方法必须申明自己的泛型
public class SePersion <E>{
    private E e;

    public E getP() {
        return e;
    }

    public void setP(E e) {
        this.e = e;
    }
    public <T> void show(T t){
        System.out.println(t);
    }
    public static<Q> void print(Q q){
        System.out.println(q);
    }
}

泛型高级之通配符

  • 泛型通配符<?>, 如果没有明确, 那么就是Object 及任意Java类
  • ? extends E ?是E的子类, 向下限定示例是这个
  • ? super E ? 是E的父类, 向下限定
public class Person {
    private String name;
    private int age;
    public  Person(){}
    public Person(String name, int age){
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public boolean equals(Object obj) {
        Person p = (Person) obj;
        boolean d = false;
        if(this.getAge() == p.getAge() && this.getName() == p.getName()){
            d = true;
        } else{
            d = false;
        }
        return d;
    }
    @Override
    public String toString(){
        return this.getName() + this.getAge();
    }
}
public class Student extends Person{
    private String name;
    private int age;
    public Student(){ }
    public Student(String name, int age){
        super(name,age);
    }
}
public class Demo5_Generic {
    public static void main(String[] args) {
        ArrayList <Person> arrayList = new ArrayList<>();
        arrayList.add(new Person("小明", 23));
        arrayList.add(new Person("小红", 22));

        ArrayList <Student> arrayList2 = new ArrayList<>();
        arrayList2.add(new Student("小张", 25));
        arrayList2.add(new Student("小美", 26));

        arrayList.addAll(arrayList2);

        System.out.println(arrayList);

        Person p = new Student("test",20);
        System.out.println(p.toString());

    }
}

增加for循环

  • 底层依赖 iterator 迭代器
import com.lizicai.bean.Person;

import java.util.ArrayList;

public class Demo6_Generic {
    public static void main(String[] args) {
        ArrayList <Person> arrayList = new ArrayList<>();
        arrayList.add(new Person("小红", 22));
        arrayList.add(new Person("小明", 23));
        arrayList.add(new Person("小张", 25));

        for(Person p:arrayList){
            System.out.println(p);
        }

        int [] arr = {1, 2, 3 ,4, 5};
        for(int i:arr){
            System.out.print(i);
        }
    }
}

三种迭代

  • 普通for循环, 可以删除, 索引-1
  • 迭代器, 可以删除, 必须使用迭代器中的remove() 方法, 否则出现并发修改异常
  • 增加for循环, 不可删除
import java.util.ArrayList;
import java.util.ListIterator;

public class Demo7_Generic {
    public static void main(String[] args) {
        ArrayList <String> arrayList = new ArrayList<>();
        arrayList.add("a");
        arrayList.add("b");
        arrayList.add("b");
        arrayList.add("c");
        for (int i = 0; i < arrayList.size(); i++) {
            if("b".equals(arrayList.get(i))){
                arrayList.remove(i);
                i--;
            }
        }

        ArrayList<String> arrayList2 = new ArrayList<>();
        arrayList2.add("a");
        arrayList2.add("b");
        arrayList2.add("b");
        arrayList2.add("c");
        ListIterator<String > it = arrayList2.listIterator();
        while (it.hasNext()){
            if( "b".equals(it.next())){
//                arrayList2.remove("b");
                it.remove();
            }
        }
        System.out.println(arrayList);

        System.out.println(arrayList2);

        for (ListIterator<String> it2 = arrayList2.listIterator(); it2.hasNext();){

            System.out.println(it2.next());
        }

    }
}

静态导入, 一般不用

import java.util.Arrays.sort

可变参数

  • 定义的时候不知道定义多少个
  • 可变参数其实是一个数组
  • 如果一个方法有多个参数, 可变参数必须是最后一个
public class Demo1_ChangeableArgs {
    public static void main(String[] args) {
       int[] arr = {5, 2, 3, 4, 5};
//       print(arr);
//       print(1,2,3,4,5,6);

//        for(int i=0;i<arr.length;i++){
//            System.out.println(arr[i]);
//        }
        pp(arr);

    }
//    public static void print(int[] arr){
//        for(int i=0;i<arr.length;i++){
//            System.out.println(arr[i]);
//        }
//    }

    public static void print(int ... arr){
        for(int i=0;i<arr.length;i++){
            System.out.println(arr[i]);
        }
    }

    public static void pp(int [] arr){
        for(int i:arr){
            System.out.println(arr[i]);
        }
    }
}

数组转集合

  • 基本数据类型数组转成集合, 只会把数组的地址作为元素转到集合中
  • 要想基本数据转成集合, 必须是引用类型 int -> Integer boolean Boolean
import java.util.Arrays;
import java.util.List;
public class Demo8_Generic {
    public static void main(String[] args) {
        String [] arr = {"a", "b", "c"};
        List<String> arrayList = Arrays.asList(arr);
//        arrayList.add("d");
        System.out.println(arrayList);

        Integer [] arr2 = {new Integer(1),new Integer(2),new Integer(3)};
        List a = Arrays.asList(arr2);
        System.out.println(a);

    }
}

集合转数组

  • 如果集合size大于等于数组, 则转换后, 数组长度 = 集合长度
  • 集合size小于数组, 转换后, 数组长度还是原数组长度
import java.util.ArrayList;

public class Demo9_Generic {
    public static void main(String[] args) {
        ArrayList<String>  arrayList = new ArrayList<>();
        arrayList.add("a");
        arrayList.add("b");
        arrayList.add("c");

        String[] arr = arrayList.toArray(new String[2]);
        String[] arr2 = arrayList.toArray(new String[4]);
        System.out.println(arr.length);
        System.out.println(arr2.length);
        for(String s:arr){
            System.out.print(s);
        }
    }
}

集合嵌套之ArrayList嵌套ArrayList

import java.util.ArrayList;

public class Demo10_Generic {
    public static void main(String[] args) {
        ArrayList <ArrayList<Person>> arrayList = new ArrayList<>();
        ArrayList<Person> cls1 = new ArrayList<>();
        ArrayList<Person> cls2 = new ArrayList<>();

        cls1.add(new Person("小明", 20));
        cls1.add(new Person("小红", 19));
        cls1.add(new Person("小张", 21));

        cls2.add(new Person("小丽", 21));
        cls2.add(new Person("小李", 22));
        cls2.add(new Person("小王", 23));

        arrayList.add(cls1);
        arrayList.add(cls2);

        for(ArrayList<Person> s:arrayList){
            for(Person p:s){
                System.out.println(p);
            }
        }
    }
}