package com.lovecoding.day19.list01;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class TestList {

    /*
    1、添加元素
    * void add(int index, E ele)
    * boolean addAll(int index, Collection<? extends E> eles)
    2、获取元素
    * E get(int index)
    * List subList(int fromIndex, int toIndex)
    3、获取元素索引
    * int indexOf(Object obj)
    * int lastIndexOf(Object obj)
    4、删除和替换元素
    * E remove(int index)
    * E set(int index, E ele)
     */

    //E set(int index, E ele)
    //E remove(int index)
    @Test
    public void test06(){
        List<Integer> list = List.of(1, 2, 3, 4, 5, 5, 6, 7); //不可变
        //构造方法
        ArrayList<Integer> integers = new ArrayList<>(list);

        integers.remove(1);

        System.out.println(integers);
        //修改 替换
        Integer set = integers.set(0, 0);
        System.out.println(set);

        System.out.println(integers);

    }


    //int indexOf(Object obj)
    //int lastIndexOf(Object obj)
    @Test
    public void test05(){
        List<Integer> list = List.of(1, 2, 3, 4, 5, 5, 6, 7); //不可变
        //boolean add = list.add(1);

        int i = list.indexOf(5);

        int i1 = list.lastIndexOf(5);
        System.out.println(i);
        System.out.println(i1);
    }


    //List subList(int fromIndex, int toIndex)
    @Test
    public void test04(){
        List<String> list= new ArrayList<>();
        list.add("1");
        list.add("2");
        list.add("3");

        List<String> list1 = list.subList(0, 1);
        System.out.println(list1);
    }
    //E get(int index)
    @Test
    public void test03(){
        List<String> list= new ArrayList<>();
        list.add("1");
        list.add("2");
        list.add("3");

        System.out.println(list.get(0));
        System.out.println(list.get(1));
        System.out.println(list.get(2));
        //System.out.println(list.get(3));

        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
    }



    //boolean addAll(int index, Collection<? extends E> eles)
    @Test
    public void test02(){
        List<String> list1= new ArrayList<>();
        list1.add("1");
        list1.add("2");
        list1.add("3");
        List<String> list = List.of("3", "4", "5");
        List<String> list2= new ArrayList<>(list);

        boolean b = list1.addAll(list2);

        System.out.println(list1);

    }

    //void add(int index, E ele)
    @Test
    public void test01(){
        List<Integer> list = new ArrayList<>();

        list.add(1);
        list.add(1,2);
        list.add(2,3);
        //不能有间断 连续的 索引添加
        //list.add(4,4);

        System.out.println(list);
    }
}
