1 PackageNet.bigwrok;2 3 Importjava.util.ArrayList;4 Importjava.util.Arrays;5 Importjava.util.LinkedList;6 Importjava.util.List;7 ImportJava.util.ListIterator;8 9 Public classCopytest {Ten Static voidReverse (list<integer>list) { Onelistiterator<integer> fwd =list.listiterator (); AListiterator<integer> rev =List.listiterator (List.size ()); - intMID = List.size ()/2;//list.size () >> 1; - for(inti = 0; I < mid; i++) { theInteger tmp =Fwd.next (); - Fwd.set (Rev.previous ()); - Rev.set (TMP); - } + } - Static voidReverse2 (list<integer>list) { + //only output it.previous (), first output element index is (size11-1) A intSize11 =list.size (); atListiterator<integer> it =List.listiterator (SIZE11); - while(It.hasprevious ()) { -System.out.print (it.previous () + ","); - } - System.out.println (); - } in - Public Static voidMain (string[] args) { tolist<integer> src = arrays.aslist (1, 2, 3, 4, 5, 6, 7, 8, 9); +List<integer> dest =NewLinkedlist<integer>(SRC); - theSystem.out.println ("Source:" +src); *SYSTEM.OUT.PRINTLN ("Destination:" +dest); $ Panax Notoginseng reverse (dest); - //Reverse2 (dest); theSystem.out.println ("Source:" +src); +SYSTEM.OUT.PRINTLN ("Destination:" +dest); A the + - } $ $}
Results
1 Source: [1, 2, 3, 4, 5, 6, 7, 8, 9]2 destination: [1, 2, 3, 4, 5, 6, 7, 8, 9]3< /c10> Source: [1, 2, 3, 4, 5, 6, 7, 8, 9]4 destination: [9, 8, 7, 6, 5, 4, 3, 2, 1]
Java programming thought Listiterator reverse order