1 Packagecom.jdk7.chapter4;2 3 Importjava.util.ArrayList;4 Importjava.util.LinkedList;5 Importjava.util.List;6 ImportJava.util.ListIterator;7 ImportJava.util.Stack;8 ImportJava.util.Vector;9 /**Ten * List of forms of collections in Java (sequential allow repetition) One * List of three forms of expression ArrayList, Vector, LinkedList A * where stack extends Vector - * Another: The list of Listiterator can manipulate the listing elements - * @authorAdministrator the * - */ - Public classListtest { - Public Static voidinit (List list) { + if(list!=NULL){ -List.add ("AAA"); +List.add ("BBB"); AList.add ("ddd"); atList.add ("Eee"); - } - } - - Public Static voidprintlist (List list) { - if(list!=NULL){ in for(intI=0;i<list.size (); i++){ -System.out.print (List.get (i) + ""); to } + } - System.out.println (); the } * $ Public Static voidarrayList () {Panax NotoginsengList arrayList =NewArrayList (); - init (arrayList); the printlist (arrayList); + } A the Public Static voidvector () { +List List =NewVector (); - init (list); $ printlist (list); $ } - - Public Static voidLinkedList () { theList List =NewLinkedList (); - init (list);Wuyi printlist (list); the } - Wu Public Static voidMain (string[] args) { -System.out.println ("===============arraylist collection =================="); About arrayList (); $System.out.println ("===============vector collection =================="); - vector (); -System.out.println ("===============linkedlist collection =================="); - LinkedList (); ASystem.out.println ("=============== operation ArrayList collection element =================="); +List List =NewArrayList (); theList.add ("1111"); -List.add ("2222"); $List.add ("3333"); theList.add (0, "eeee"); theList.indexof ("2222"); the printlist (list); theSystem.out.println ("================================="); -List List1 =NewArrayList (); in List1.addall (list); theList1.add ("BBBB"); the printlist (list1); AboutSystem.out.println ("Array list1 contains array list:" +List1.containsall (list)); theList1.set (1, "VVVV"); the printlist (list1); theSystem.out.println ("===============list's walker implements the operation of the collection element =================="); +Listiterator Listit =list.listiterator (); -System.out.print ("Forward traversal Result:"); the while(Listit.hasnext ()) {BayiSystem.out.print (Listit.next () + ""); the } the System.out.println (); -System.out.print ("Reverse Traversal Result:"); - while(Listit.hasprevious ()) { theSystem.out.print (listit.previous () + ""); the } the System.out.println (); the //Operation of the array elements at the position of the Walker, the position of the walker is changed by Listiterator's next () and previous () - Listit.next (); theListit.add ("FFFF"); the printlist (list); theSystem.out.println ("=================================");94 Listit.next (); the Listit.remove (); the printlist (list); theSystem.out.println ("===============vector class's subclass stack class operations collection element ==================");98Stack Mystack =NewStack (); AboutMystack.push ("HHhH"); -Mystack.push ("Eeee");101Mystack.push ("Llll");102Mystack.push ("Oooo");103 printlist (mystack);104System.out.println ("================ stack trailer element and stack all elements ================="); theSYSTEM.OUT.PRINTLN ("Stack trailing element:" +Mystack.peek ());106 printlist (mystack);107System.out.println ("================ pops up the stack trailing elements and stacks all elements =================");108System.out.println ("Popup Stack Trailing element:" +Mystack.pop ());109 printlist (mystack); theSystem.out.println ("================ operation LinkedList Collection Element =================");111LinkedList LinkedList =NewLinkedList (); the Linkedlist.addall (list);113 printlist (linkedlist); theSYSTEM.OUT.PRINTLN (insert ' start ' at the head of the collection)); theLinkedlist.addfirst ("Start"); the printlist (linkedlist);117System.out.println ("Insert ' end ' at collection tail");118Linkedlist.addlast ("End");119 printlist (linkedlist); -System.out.println ("Delete collection header element ' start '");121 Linkedlist.removefirst ();122 printlist (linkedlist);123System.out.println ("Delete end of collection trailing element '");124 linkedlist.removelast (); the printlist (linkedlist);126SYSTEM.OUT.PRINTLN ("Set insert element ' AAAA '");127Linkedlist.addfirst ("AAAA"); -Linkedlist.addlast ("AAAA");129 printlist (linkedlist); theSystem.out.println ("delete element ' AAAA '"));131Linkedlist.remove ("AAAA"); the printlist (linkedlist);133 }134 }135 136 137 The result of the execution is:138===============arraylist Collection ==================139 aaa BBB DDD Eee $===============vector Collection ==================141 aaa BBB DDD Eee142===============linkedlist Collection ==================143 aaa BBB DDD Eee144=============== manipulating ArrayList collection elements ==================145Eeee 1111 2222 3333146=================================147Eeee 1111 2222 3333bbbb148The array list1 contains the array list:true149Eeee VVVV 2222 3333bbbb Max===============list's Walker implements the operation of the collection element ==================151Forward traversal results: eeee 1111 2222 3333 theReverse traversal results: 3333 2222 1111eeee153Eeee FFFF 1111 2222 3333154=================================155Eeee FFFF 2222 3333156The ===============vector class's subclass stack class operations collection element ==================157 hhhh eeee llll oooo158================ stack tail elements and stack all elements =================159 stack trailing element: Oooo the hhhh eeee llll oooo161================ pops up the stack trailing elements and stacks all elements =================162 pop-up stack trailing element: Oooo163 hhhh eeee llll164================ manipulating LinkedList collection elements =================165Eeee FFFF 2222 3333166Insert ' Start ' at the head of the collection167Start eeee ffff 2222 3333168Insert ' end ' at the tail of the collection169Start eeee ffff 2222 3333End theDelete Collection header element ' start '171Eeee FFFF 2222 3333End172Delete Collection trailing element ' end '173Eeee FFFF 2222 3333174Set insert element ' AAAA '175AAAA eeee FFFF 2222 3333AAAA176Delete element ' AAAA '177Eeee ffff 2222 3333 AAAA
List of Java collections: ArrayList, vectors, LinkedList