码迷,mamicode.com
首页 > 编程语言 > 详细

java编程思想 ListIterator 逆序

时间:2018-06-03 14:26:18      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:style   java编程   class   div   string   previous   TE   net   arraylist   

 

 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 逆序

标签:style   java编程   class   div   string   previous   TE   net   arraylist   

原文地址:https://www.cnblogs.com/kwaitfort/p/9128855.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!