8.2笔记

1. Collection

Collection是一个接口,是无法直接实例化的,但是有一个可以实例化Collection的类ArrayList

public  static void main(String[] args) {
        Collection<String> test = new ArrayList<>();
        
    }
1.1 Collection下面的方法:

增: add, addAll

 public static void main(String[] args) {
        Collection<String> test = new ArrayList<>();
        Collection<String> test1 = new ArrayList<>();
        test1.add("a");
        test1.add("b");
        test1.add("c");
        test1.add("f");
        test1.add("d");
        test.addAll(test1);
        System.out.println(test);
    }

删:remove,removeAll

  public static void main(String[] args) {
        Collection<String> test = new ArrayList<>();
        Collections.addAll(test, "a","b","c","d");
        Collection<String> test1 = new ArrayList<>();
        test1.add("b");
        test1.add("e");
        test.remove("a");
        System.out.println(test);
        boolean flag2 = test.removeAll(test1);
        System.out.println(test);

clear

 public static void main(String[] args) {
        Collection<Integer> test = new ArrayList<>();
        Collections.addAll(test,1,2,3,4);
        System.out.println(test);
        test.clear();
        System.out.println(test);
    }

查:size, toArray , contains , containsAll , isEmpty

public static void main(String[] args) {
        System.out.println(test1());
//        System.out.println(test.size());
    }
    public static int  test1(){
        Collection<String> test = new ArrayList<>();
        test.add("a");
        test.add("b");
        Collections.addAll(test,"c","d","e");
        return test.size();
    }
    public static void main(String[] args) {
        Collection<Integer> test = new ArrayList<>();
        test.add(1);
        test.add(2);
        test.add(3);
        test.add(4);
        Collection<Integer> test1 = new ArrayList<>();
        test1.add(1);
        test1.add(2);
        System.out.println(test.containsAll(test1));
        System.out.println(test.isEmpty());
    }
1.2 Collection的数据遍历
1.2.1 for循环遍历
public static void main(String[] args) {
        Collection<Character> test = new ArrayList<>();
        test.add('君');
        test.add('不');
        Collections.addAll(test,'见','黄','河','之','水','天','上','来');
        System.out.println(test);
        Character[] arr =test.toArray(new Character[test.size()]);
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
            
        }
1.2.2 增强for循环遍历
    public static void main(String[] args) {
        Collection<Character> test = new ArrayList<>();
        test.add('君');
        test.add('不');
        Collections.addAll(test,'见','黄','河','之','水','天','上','来');
        //(数据的类型 ,临时的变量名 : 集合的名称)
        for (Character character : test) {
            System.out.println(character);
        }
    }
1.2.3 迭代器遍历
   public static void main(String[] args) {
        Collection<Character> test = new ArrayList<>();
        test.add('君');
        test.add('不');
        Collections.addAll(test,'见','黄','河','之','水','天','上','来');
        Iterator<Character> iterator = test.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }

2. List

List 是Collection的一个子接口,也无法直接进行实例化,需要它的子类ArrayList来进行实例化,也就是多态的形式。

  public static void main(String[] args) {
        List<String> list = new ArrayList<>();
    }
1.1 List下Collection没有的方法:

增:add(int index ,E element) // 将指定的元素添加到指定的下标中

​ addAll(int index , E element) // 将指定的集合添加到指定的下标中

public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("1");
        list.add("2");
        list.add(1,"3");
        System.out.println(list);
        List<String> list1 = new ArrayList<>();
        list1.add("4");
        list1.add("5");
        list.addAll(2,list1);
    }

删 :E remove(int index) //通过下标进行删除,返回值是删除的那个数据。

List<String> list1 = new ArrayList<>();
        list1.add("4");
        list1.add("5");
        list.addAll(2,list1);
        System.out.println(list);
        //删除索引下标为2的元素
        String remove = list.remove(2);
        System.out.println(remove);
        System.out.println(list);

改:E set(int index, E e) //通过指定的索引修改数据元素,返回值是被修改的原数据

   //修改索引下标为1的元素,返回值为被修改的数据
        String li = list.set(1, "li");
        System.out.println(li);
        System.out.println(list);

查:E get(int index); //通过索引下标去获取指定元素

​ int indexOf(Object obj);//通过元素获取指定的元素下标

​ List subList(int formIndex, int toIndex); //截取一部分元素数据 form为开头, to为结尾。

 String li = list.set(1, "li");
        System.out.println(li);
        System.out.println(list);
        //通过下标获取元素数据
        System.out.println(list.get(2));
        //截取一部分数据元素
        System.out.println(list.subList(2, 4));
1.2 List的数据遍历
1.2.1 for循环遍历
 public static void main(String[] args) {
        List<Integer> arr = new ArrayList<>();
        arr.add(1);
        arr.add(2);
        arr.add(3);
        System.out.println(arr);
        for (int i = 0; i <arr.size() ; i++) {
            System.out.println(arr.get(i));
        }
    }
1.2.2 增强for循环遍历
 public static void main(String[] args) {
        List<Integer> arr = new ArrayList<>();
        arr.add(1);
        arr.add(2);
        arr.add(3);
        System.out.println(arr);
        for (Integer integer : arr) {
            System.out.println(integer);
        }
1.2.3 迭代器遍历
 public static void main(String[] args) {
        List<Integer> arr = new ArrayList<>();
        arr.add(1);
        arr.add(2);
        arr.add(3);
        System.out.println(arr);
        ListIterator<Integer> ill = arr.listIterator(arr.size());
        while (ill.hasNext()){
            System.out.println(ill.next());
        }
        while (ill.hasPrevious()){
            System.out.println(ill.previous());
        }

3.集合中存对象


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;
    }
//重写toString方法
    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
public class Demo8 {
    public static void main(String[] args) {
        Collection<Person> list = new ArrayList<>();
        list.add(new Person("张三",5));
        System.out.println(list);
        List<Person> list1 = new ArrayList<>();
        list1.addAll(list);
        list1.add(new Person("王五",6));
        list1.add(new Person("彩云",8));
        list1.add(1,new Person("李四",7));
        System.out.println(list1);

    }
}

4. ArrayList 和 LinkedList区别:

​ ArrayList和Vector 底层是数组,但是Vector是线程安全的,所以效率底,开发中不用Vector
​ 接下来介绍ArrayList和linkedListu区别:
​ 1.ArrayList底层是数组
​ LinkedList底层是双向链表
​ 2.ArrayList 的特征:
​ 查询快: 底层是索引,有序的通过内存有序地址,直接找到 时间复杂度 o(1)
​ 增删慢: 增加数据和删除数据有可能扩容。每次扩1.5倍。扩容的话,浪费内存浪费时间o(n)
​ 3.LinkedList的特征:
​ 查询慢:底层是二分法查找的算法 时间复杂度是log以2为底 n的对数
​ 增删快:直接找前置结点 Node prev,后继结点 Node next
​ 。时间复杂度是 o(1)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值