JAVA Study Notes (20)-collection List, java Study Notes

Source: Internet
Author: User

JAVA Study Notes (20)-collection List, java Study Notes
ArrayList set

/** ArrayList, which implements the List interface *. When declaring an array, you must specify the element type in the array, which can be the basic data type, it can also be a reference data type * When declaring a set, you can not specify the type of its elements. The default element type is the Object type */public class Test01 {public static void main (String [] args) {// create an ArrayList set ArrayList list = new ArrayList (); // Add the element list. add ("tom"); list. add (25); list. add (1, 12.4); list. add (true); list. add (new Student (); list. add ("tom"); // features of the elements in the List set: ordered, allow repeated list. add (1, "alice"); // list. add (1, 20,250); list. Add (null); // modify the element list. set (0, "mike"); // Delete the element list. remove (1); list. remove ("tom"); list. remove (new Integer (25); // list. clear (); // clear the elements in the Set // obtain the elements in the Set/** Object obj = list. get (2); // The get () method returns an Object boolean flag = (Boolean) * obj; */boolean flag = (Boolean) list. get (2); System. out. println ("element of the SET index 2:" + flag); // you can determine whether the set includes the specified System element. out. println ("tom? "+ List. contains ("tom"); System. out. println ("whether the set is empty:" + list. isEmpty (); System. out. println ("index of mike in the Set:" + list. indexOf ("mike"); System. out. println ("number of elements:" + list. size (); // The element for (int I = 0; I <list. size (); I ++) {System. out. println (list. get (I); // get the elements in the set through the get () method} // convert the set to an array Object [] objs = list. toArray (); System. out. println ("element in the array:" + Arrays. toString (objs ));}}
ArrayList collection instance
/** ArrayList */public class Test02 {public static void main (String [] args) {// create a collection List for storing dog information = new ArrayList (); dog dog1 = new Dog ("wangcai", "Wolf Dog", 2); Dog dog2 = new Dog ("sunspot", "Labrado", 1 ); dog dog3 = new Dog ("Xiaomei", "xuenarui", 5); // Add the Dog object to the list in the collection. add (dog1); list. add (dog2); list. add (dog3); // query the number of dogs in the System. out. println ("Total" + list. size () + "dog! "); System. out. println ("respectively:"); for (Object obj: list) // use the foreach loop to traverse the set {Dog dog = (Dog) obj; // The default element type in the set is Object, which must be converted to System. out. println (dog. getName () + "," + dog. getBrand () + "," + dog. getAge () ;}}/ ** Dog class */class Dog {private String name; private String brand; private int age; public Dog (String name, String brand, int age) {super (); this. name = name; this. brand = brand; this. age = age;} public String getName () {return name;} public void setName (String name) {this. name = name;} public String getBrand () {return brand;} public void setBrand (String brand) {this. brand = brand;} public int getAge () {return age;} public void setAge (int age) {this. age = age ;}}
Vectoring set
/** Vectoring: similar to ArrayList, It is thread-safe, but its efficiency and performance are not as good as that of ArrayList, it is not recommended to use */public class Test03 {public static void main (String [] args) {// to create a Vector set Vector v = new Vector (); v. add (1, 100); v. add (new Double (6.5); v. add (new Date (); v. add ("hello"); v. add (new String ("world"); System. out. println ("number of elements:" + v. size ();/** traversal Set Method * // method 1 System. out. println ("********* method 1 ********"); for (int I = 0; I <v. size (); I ++) {System. out. println (v. get (I);} // method 2 System. out. println ("******** method 2 ********"); for (Object obj: v) {System. out. println (obj);} // method 3: Use Enumeration for iteration, applicable to Vector, HashTable, Properties, and other sets, out of date System. out. println ("********* method 3 ********"); Enumeration e = v. elements (); // call the elements () method of Vector and return an Enumeration instance while (e. hasMoreElements () {// call the hasMoreElements () method of Enumeration to determine whether the Element Object obj = e. nextElement (); // call the nextElement () method of Enumeration to obtain the current element System. out. println (obj);} // directly outputs the System set. out. println (v );}}
Stack
Import java. util. stack;/** Stack, inherited from Vector */public class Test04 {public static void main (String [] args) {// create an empty Stack stack Stack = new stack (); // inbound Stack stack. push ("tom"); stack. push ("jack"); stack. push ("alice"); // output stack System. out. println (stack. pop ());}}
Collections list set
Import java. util. using List;/** using List, which implements the List interface and linked List storage. Frequent addition, modification, or deletion has a higher validity rate */public class Test05 {public static void main (String [] args) {// create an external list set whose list = new external list (); list. add ("itany"); list. add (12); list. add (new Double (12.5); list. add (0, "Zhao Xin"); list. addFirst ("galun"); list. addLast ("cold ice"); System. out. println ("index 2 element:" + list. get (2); System. out. println ("first element:" + list. getFirst (); System. out. println ("first element:" + list. get (0); System. out. println ("last element:" + list. getLast (); System. out. println ("last element:" + list. get (list. size ()-1); System. out. println ("element in the Set:" + list); list. removeFirst (); list. removeLast (); System. out. println ("element in the Set:" + list );}}
Performance Comparison of ArrayLis, Vector, and LinkedList
Import java. util. arrayList; import java. util. using list; import java. util. vector;/** ArrayLis, Vector, and javaslist performance comparison */public class Test06 {public static void main (String [] args) {ArrayList al = new ArrayList (); vector v = new Vector (); empty list ll = new empty list (); // test ArrayList performance long time1 = System. currentTimeMillis (); // obtain the number of milliseconds before execution for (int I = 1; I <100000; I ++) {al. add (0, I);} long time2 = System. currentTimeMillis (); // obtain the number of milliseconds after execution System. out. println ("ArrayList time spent:" + (time2-time1); // test the Vector performance long time3 = System. currentTimeMillis (); for (int I = 1; I <100000; I ++) {v. add (0, I);} long time4 = System. currentTimeMillis (); System. out. println ("time spent by the Vector:" + (time4-time3); // test the duration list performance long time5 = System. currentTimeMillis (); for (int I = 1; I <100000; I ++) {ll. addFirst (I);} long time6 = System. currentTimeMillis (); System. out. println ("elapsed list time:" + (time6-time5 ));}}

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.