List.add(int index,E element)

/**
  * Inserts the specified element at the specified position in this list
  * (optional operation).  Shifts the element currently at that position
  * (if any) and any subsequent elements to the right (adds one to their
  * indices).
  *  在list指定的位置插入指定的元素。如果当前位置存在元素,那么移动当前元素
  *  及右侧子序列到对应index+1的位置。
  */
   void add(int index, E element);

代码示例:

public class Main {

    public static void main(String[] args) {
        List list = new ArrayList<>();
        list.add("1");
        list.add("2");
        list.add("3");

		//if index > size or index < 0 ,will throw java.lang.IndexOutOfBoundsException
        list.add(2, "4");
        list.forEach(System.out::println);
    }
}

result:
1
2
4
3

你可能感兴趣的:(List.add(int index,E element))