java编程思想 ListIterator 逆序

Posted wait_for_y

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java编程思想 ListIterator 逆序相关的知识,希望对你有一定的参考价值。

 

 1 package net.bigwrok;
 2 
 3 import java.util.ArrayList;
 4 import java.util.Arrays;
 5 import java.util.LinkedList;
 6 import java.util.List;
 7 import java.util.ListIterator;
 8 
 9 public class CopyTest {
10     static void reverse(List<Integer> list) {
11         ListIterator<Integer> fwd = list.listIterator();
12         ListIterator<Integer> rev = list.listIterator(list.size());
13         int mid = list.size() / 2;// list.size() >> 1;
14         for (int i = 0; i < mid; i++) {
15             Integer tmp = fwd.next();
16             fwd.set(rev.previous());
17             rev.set(tmp);
18         }
19     }
20     static void reverse2(List<Integer> list) {
21         //仅仅输出  it.previous() ,第一次输出的元素index是 (size11 -1)
22          int size11  =  list.size();
23         ListIterator<Integer> it = list.listIterator(size11);
24         while (it.hasPrevious()) {
25             System.out.print (it.previous()+" , ");
26         }
27         System.out.println(); 
28     }
29 
30     public static void main(String[] args) {
31         List<Integer> src = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
32         List<Integer> dest = new LinkedList<Integer>(src);
33          
34         System.out.println("source: " + src);
35         System.out.println("destination: " + dest);
36         
37         reverse(dest);
38         //reverse2(dest);
39         System.out.println("source: " + src);
40         System.out.println("destination: " + dest);
41         
42          
43     
44     }
45 
46 }

 

结果

1 source: [1, 2, 3, 4, 5, 6, 7, 8, 9]
2 destination: [1, 2, 3, 4, 5, 6, 7, 8, 9]
3 source: [1, 2, 3, 4, 5, 6, 7, 8, 9]
4 destination: [9, 8, 7, 6, 5, 4, 3, 2, 1]

 

以上是关于java编程思想 ListIterator 逆序的主要内容,如果未能解决你的问题,请参考以下文章

逆序遍历List集合

逆序遍历List集合

HDU 5919 Sequence II(主席树+逆序思想)

Java ListIterator(迭代器)

Java:list.listIterator() 返回的对象属于哪个类?

java:集合输出之Iterator和ListIterator二