通过列表迭代器在子列表中添加元素
Adding an element in a sublist through a list iterator
我有一个包含元素的数组列表-
0, 100, 200, 300, 400, 500, 600, 700...
从主列表中获取子列表后,如果 next() 返回的元素是 400
,我将添加一个元素
public static void add(List<String> list){
List<String> subList = list.subList(2, 7);// 200, 300, 400, 500, 600
ListIterator<String> listIterator = subList.listIterator();
while(listIterator.hasNext()) {
String value = listIterator.next();
if(value.equals("400")) {
listIterator.add("399");
}
}
System.out.println(subList);
}
子列表现在变成 -
[200, 300, 400, 399, 500, 600]
可见,元素 399
是 after 400
.
doc 表示
Inserts the specified element into the list (optional operation). The
element is inserted immediately before the element that would be
returned by next()....
请说明。
Inserts the specified element into the list (optional operation). The element is inserted immediately before the element that would be returned by next()
这意味着该元素将被插入到 next 对 next()
的调用返回的元素之前(即对 next()
的调用发生在 在 对 listIterator.add("399")
的调用之后,而不是在它之前),即 500。因此新元素在 500 之前添加。
immediately before the element that would be returned by next()
此时 next
返回的元素是 500。
Inserts the specified element into the list (optional operation). The element is inserted immediately before the element that would be returned by next().
public static void add(List<String> list){
List<String> subList = list.subList(2, 7);// 200, 300, 400, 500, 600
ListIterator<String> listIterator = subList.listIterator();
while(listIterator.hasNext()) {
String value = listIterator.next();
if(value.equals("400")) { //when this statement is executed next would return 400
//399 will be inserted before next call to next() function , next call to next() function would return 500, so it will be inserted before 500*/
listIterator.add("399");
}
}
System.out.println(subList);
}
我有一个包含元素的数组列表-
0, 100, 200, 300, 400, 500, 600, 700...
从主列表中获取子列表后,如果 next() 返回的元素是 400
public static void add(List<String> list){
List<String> subList = list.subList(2, 7);// 200, 300, 400, 500, 600
ListIterator<String> listIterator = subList.listIterator();
while(listIterator.hasNext()) {
String value = listIterator.next();
if(value.equals("400")) {
listIterator.add("399");
}
}
System.out.println(subList);
}
子列表现在变成 -
[200, 300, 400, 399, 500, 600]
可见,元素 399
是 after 400
.
doc 表示
Inserts the specified element into the list (optional operation). The element is inserted immediately before the element that would be returned by next()....
请说明。
Inserts the specified element into the list (optional operation). The element is inserted immediately before the element that would be returned by next()
这意味着该元素将被插入到 next 对 next()
的调用返回的元素之前(即对 next()
的调用发生在 在 对 listIterator.add("399")
的调用之后,而不是在它之前),即 500。因此新元素在 500 之前添加。
immediately before the element that would be returned by
next()
此时 next
返回的元素是 500。
Inserts the specified element into the list (optional operation). The element is inserted immediately before the element that would be returned by next().
public static void add(List<String> list){
List<String> subList = list.subList(2, 7);// 200, 300, 400, 500, 600
ListIterator<String> listIterator = subList.listIterator();
while(listIterator.hasNext()) {
String value = listIterator.next();
if(value.equals("400")) { //when this statement is executed next would return 400
//399 will be inserted before next call to next() function , next call to next() function would return 500, so it will be inserted before 500*/
listIterator.add("399");
}
}
System.out.println(subList);
}