📜  arraylist 如何在 0 处添加 java 代码示例

📅  最后修改于: 2022-03-11 14:52:39.705000             🧑  作者: Mango

代码示例1
//The add(int index, E element) method of Java ArrayList class 
//inserts a specific element in a specific index of ArrayList. 
//It shifts the element of indicated 
//index if exist and subsequent elements to the right.
List colors = new ArrayList<>();
colors.add("red");      // ["red"]  
colors.add("blue");     // ["red" , "blue"]  
colors.add(1, "white"); // ["red" , "white", "blue"]  
colors.add(0, "black"); // ["black", "red" , "white", "blue"]