天天看点

持有对象 练习12

将第一个表反序插入第二个表

import java.util.*;

public class Ex12 {

 public static void main(String[] args) {

  List<Integer> li1 =

   new ArrayList<Integer>(Arrays.asList(0, 1, 2, 3, 4));

  List<Integer> li2 =

   new ArrayList<Integer>(Arrays.asList(5, 6, 7, 8, 9));

  // start it1 at the end:

  ListIterator<Integer> it1 = li1.listIterator(5);

  ListIterator<Integer> it2 = li2.listIterator();

  System.out.println("li1: " + li1);

  System.out.println("li2: " + li2);

  // now use it2 to re-set li2

  while(it2.hasNext()) {

   it2.next();

   it2.set(it1.previous());

  }

  System.out.println("li1: " + li1);

  System.out.println("li2: " + li2);

 }

}

输出

G:/text/Holding>java Ex12

li1: [0, 1, 2, 3, 4]

li2: [5, 6, 7, 8, 9]

li1: [0, 1, 2, 3, 4]

li2: [4, 3, 2, 1, 0]

上一篇: POJ 2325