import java.util.List;
import java.util.ArrayList;
public class OpList {
public static void main(String args[]) {
int i;
List<String> testList;
testList = new ArrayList<String>();
testList.add("the first element");
testList.add("the second element");
testList.add("the third element");
testList.add("the fourth element");
// change the sequence
testList.remove("the third element");
testList.add(1, "the third element");
for(i = 0; i < testList.size(); i++)
System.out.println(testList.get(i));
}
}
the result:
xxx:~/java/oplist$ java OpList
the first element
the third element
the second element
the fourth element
the sequence before changing the sequence:
the first element
the second element
the third element
the fourth element
在一个List中的指定位置插入一个元素,当前指定位置的元素会往后面移动一个位置。