ArrayList

泛型

TIP

<E>泛型 指的是 装在集合中 所有的元素, 全部都是统一的什么类型 引用类型, 基本类型不可以
限定这个集合存储的数据类型
ArrayList<String> arr1 = new ArrayList<>(); ArrayList arr1 = new ArrayList(); arr1.add("str"); <String> 指的是arr1 中的元素必须都是字符串类型 将运行期的错误 提前到编译器检查 不用强转

  • 提高安全性
  • 省去强转的麻烦

添加

arr1.add("abc");

  • add 方法是有返回值的 true,对于 ArrayList add方法一定是成功的, 对于其它集合不一定成功

获取

arr1.get(index)

  • index 索引值 从0 开始

删除

arr1.remove(index);
arr1.removeAll(arr2); 清除交集

  • index int类型

交集

arr1.retainAll(c2);

包含

arr1.containsAll(arr2)

长度

arr1.size();

存储数字

存储基本类型 需要使用 包装类 byte short int lonfg float 都变了char boolean

equals和==

  1. == 能比较基本数据类型也能比较引用数据类型
    • 基本数据类型比较的是值
    • 引用数据类型比较的是地址
  2. equals 只能比较 引用数据类型
    • 没重写之前 比较的是地址值, 底层依赖的是 == 号, 但是比较地址是没有意义的, 我们需要重写equals 方法 比较对象的属性值.

迭代器

集合是用来存储元素的, 存储的元素需要查看, 那么就需要迭代(遍历)

自动类型提升

    Collection arr1 = new ArrayList();
    arr1.add(new Student("aa", 12));  // Object obj = new Student();  自动提升为object 
    arr1.add(new Student("dd", 6));
    arr1.add(new Student("cc", 4));
    arr1.add(new Student("ee", 1));
    Iterator it = arr1.iterator();
    Object b = null;
    while(it.hasNext()){
        b = it.next();
        Student b1 = (Student)b;
        System.out.println(b1.getName()+"+"+b1.getAge());

    }
1
2
3
4
5
6
7
8
9
10
11
12
13

迭代器原理

TIP

迭代器是对集合的遍历. 而每一个集合的内部的存储接口都是不同的, 所以每个集合的存和取都是不一样的, 那么就需要在每一个类中定义 hasNext(), next() 方法, 这样做可以 , 但是会让代码过于臃肿,
迭代器是将这一的方法向上抽取出接口, 然后在每个类内部, 定义自己的迭代方式, 这样做的方式好处有二, 1, 规定了整体集合体系的遍历方式hasNext() next()方法, 2. 代码由底层实现, 使用者不管怎么实现, 会用即可

数组去重

public static ArrayList getSingle1 (ArrayList oldArr){
    ArrayList arr = new ArrayList<>();
    for (int i = 0; i < oldArr.size(); i++) {
        if( !arr.contains( oldArr.get(i) ) ){
            arr.add( oldArr.get(i) );
        };
    }
    return  arr;
}
/**********************************************/

public static ArrayList getSingle2 (ArrayList old) {
    ArrayList arr = new ArrayList();
    Iterator it = old.iterator();
    Object obg = null;


    while (it.hasNext()) {
        obg = it.next();
        if(!arr.contains(obg)){
            arr.add(obg);
        }
    }
    return arr;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

contains

`contains 依赖的  equals, 而Objec equals 没重写之前 判断的是 内存地址`
























 
 
 
 
 
 



























public class Person {
    private String name;
    private  int age;

    public Person(String name, int age) {
        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 boolean equals(Object o) {
       Person p = (Person)o;
        return this.name.equals(p.name) && this.age == p.age;
    }
}


public class Test3 {
    public static void main(String[] args) {
        ArrayList list = new ArrayList();
        list.add(new Person("张三", 22));
        list.add(new Person("李四", 23));
        list.add(new Person("王五", 21));
        list.add(new Person("李四", 23));
        System.out.println(list);
        System.out.println(getSingel(list));
    }

    public static ArrayList getSingel (ArrayList old){
        ArrayList arr = new ArrayList();
        Iterator it = old.iterator();
        Object ob;
        while (it.hasNext()){
            ob = it.next();
            if(! arr.contains( ob ) ){
                arr.add(ob);
            }
        }
        return arr;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
Last Updated: 8/4/2020, 6:23:45 PM