Java Learning Lesson 35th (Common Object API)-collection Framework (iii)-vector, LinkedList, ArrayList collection demo

Source: Internet
Author: User

Set Frame composition diagram


Excerpt from Baidu Pictures

First, Vector collection demo

Although vectors are not commonly used, it is still useful to know the methods

Import Java.util.arraylist;import java.util.enumeration;import java.util.iterator;import java.util.LinkedList; Import java.util.list;import java.util.listiterator;import java.util.vector;import java.util.*;p ublic class Main { public static void Main (string[] args) {vector vec = new vector (), Vec.add ("A1"), Vec.add ("A2"), Vec.add ("A3"); Vec.add ("A4 "); Enumeration Enumer = Vec.elements (); while (Enumer.hasmoreelements ()) {Object object = (Object) enumer.nextelement (); System.out.println ("Next Elements" +object); /* * The functionality of this interface is duplicated with the functionality of the Iterator interface. Additionally, the Iterator interface adds an optional remove operation and uses a shorter method name. * The new implementation should prioritize the use of the Iterator interface instead of the enumeration interface. * */iterator it = Vec.iterator (); while (It.hasnext ()) {Object object = (Object) it.next (); System.out.println ("It Next" +object);}}}

Second, LinkedList collection demo

Import Java.util.linkedlist;public class Main {public static void Main (string[] args) {LinkedList link = new LinkedList (); Link.addfirst ("A1"), Link.addfirst ("A2"), Link.addfirst ("A3"), Link.addfirst ("A4");/* head interpolation */system.out.println (" link = "+link");//system.out.println ("Get First elments =" +link.getfirst ());//Gets the initial element//system.out.println ("Remove Frist elements = "+link.removefirst ());//delete the first element/*while (!link.isempty ()) {//start from the beginning is traversal, take a delete one System.out.println ( Link.removefirst ());} */while (!link.isempty ()) {//From the end of the Loop System.out.println (Link.removelast ());} System.out.println ("Finally link =" +link);}}

API Documentation Explanation: Note that this implementation is not synchronous. If multiple threads access a linked list at the same time, and at least one of the threads modifies the list from the fabric, it must remain externally synchronized. (A structural modification is any action that adds or removes one or more elements; The value of the set element is not a structural modification.) This is typically done by synchronizing the objects that naturally encapsulate the list. If such an object does not exist, you should use the Collections.synchronizedList method to "wrap" the list. It's a good idea to do this at creation time to prevent accidental, out-of-sync access to the list


Link's stack and queue presentation

Import java.util.linkedlist;class queue{private linkedlist link;public Queue () {//TODO auto-generated constructor Stublink = new LinkedList ();} public void Addqueele (Object obj) {link.addlast (obj);} Public Object Getqueele () {return Link.removefirst ();//stack just change to Link.removelast () to}public Boolean isqueempty () {return Link.isempty ();}} public class Main {public static void main (string[] args) {Queue Q = new Queue (); Q.addqueele ("A1"); Q.addqueele ("A2"); Q.addqueele ("A3"); Q.addqueele ("A4"); while (! Q.isqueempty ()) {System.out.println (Q.getqueele ());}}}

link method JDK1.6:

JDK1.5 before Addfrist (); add addlast (); Jdk1.6offerfrist () offerlast (); JDK1.5 before Getfrist (); Gets the first element (not deleted), if link is empty, throws a Nosuchelementexception exception getlast (); Jdk1.6peekfrist (): Gets the first element (not deleted) if link is empty, return nullpeeklast (); Jdk1.5removefrist (): Gets the first element and deletes if link is empty, throws Nosuchelementexception exception Removelast (): Jdk1.6pullfrist (): Gets the first element and deletes it, If link is empty, return Nullpulllast ():

Three, ArrayList collection demo

Import Java.security.permissions;import java.util.arraylist;import java.util.iterator;import java.util.LinkedList; Class Man{private string name;private int age;public man (String name, Int. age) {super (); this.name = Name;this.age = age;} Public Man () {super ();//TODO auto-generated constructor stub}public String getName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;}} public class Main {public static void main (string[] args) {ArrayList alist = new ArrayList (); Alist.add (New Man ("A1", one)); a List.add (New Man ("A2")), Alist.add ("A3"), Alist.add (New man ("A4")); Iterator it = Alist.iterator (); while (It.hasnext ()) {Mans Pman = (man) it.next (); System.out.println (Pman.getname () + "::" +pman.getage ());}}}

Java Learning Lesson 35th (Common Object API)-collection Framework (iii)-vector, LinkedList, ArrayList collection demo

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.