Article 4 of the dark horse programmer series (1) and Article 4 of the dark horse

Source: Internet
Author: User

Article 4 of the dark horse programmer series (1) and Article 4 of the dark horse

ASP. Net + Android + IOS development and Net training. We look forward to communicating with you!

 

This article is based on the course video of teacher Bi Xiangdong, if you want to learn in detail, please watch the video of teacher Bi Baidu Network Disk link address: http://pan.baidu.com/s/1o6mwDzO

 

Directory: 1. Collection Overview 2. Collection and Iterator interfaces 3. List interfaces and Their ArrayList, sorted List, and Vector subclasses

 

1. Collection Overview

 

For the Framework Design Diagram integrated in JDK, we mainly use four sets in the black box.

Array and set are different: the number of objects in the set is not fixed, and the number of objects in the array must be determined.

The underlying data storage structure of ArrayList is an array structure. It features fast query speed, slow addition, deletion, and non-synchronization of threads.

The underlying data structure of the shortlist is the linked list structure. Features: Slow query and fast addition/Deletion

The bottom layer of the Vector is the structure of the array data. It is very slow, thread synchronization, and the earliest appearance. It has been replaced by ArrayList and is rarely used now.

The elements in the Set are unordered (the order in which the data is stored and retrieved is not necessarily the same), and the elements cannot be repeated.

The underlying data structure of HashSet is a hash table.

 

2. Collection and Iterator Interfaces

The Iterator is defined to facilitate the collection of operation elements. The ListIterator (A subinterface of the Iterator Interface) is defined for the List set)

Note: when using the iterator, you cannot use the method of the Set object to operate on the objects in the set. A ConcurrentModification exception occurs.

The methods in the two basic interfaces are very important.

The Code demonstrates the use of the Collection method and the traversal operation method (the example is very common, mainly look at the traversal operation method)

1 public class CollectionTest {2 public static void main (String [] args) {3 List d = new ArrayList (); // create a set and add an object 4 d to the set. add ("jake"); 5 d. add ("alean"); 6 d. add ("sam"); 7 iteratorTest (d); 8} 9 // how to use ListIterator to traverse the object 10 public static void iteratorTest (List c) {11 ListIterator it = c. listIterator (); 12 while (it. hasNext () {13 // Add 14 if (it. next ()! = Null) {15 it. add ("nanjing"); 16} 17 // modify 18 // if (it. next () = "jake") {19 // it. set ("beokj"); 20 //} 21} 22 System. out. println (c); 23} 24 // use 25 public static void collectionMethods () {26 collection c = new ArrayList (); 27 Collection d = new ArrayList (); 28 // increase by 29 d. add ("jake"); d. add ("alean"); d. add ("sam"); 30 c. addAll (d); 31 // Delete 32 // c. remove ("jake"); 33 // c. removeAll (c); 34 // c. clear (); 35 System. out. println (c. isEmpty (); 36 System. out. println (c + ": size =" + c. size (); 37} 38}

 

3. List interface and its ArrayList, struct List, and Vector subclass

The underlying method used in ArrayList to compare whether objects in a set are the same is the equals () method of objects in the set.

The method JDK1.6 in the revoke list is replaced by the getFirst () \ getLast () \ removeFirst () \ removeLast () method.

OfferFirst \ offerLast \ peekFirst () \ peekLast () is a new method. The advantage of the new method is that there is no element in the set and no NoSuchElementException exception will occur.

Vector has a unique enumeration method to retrieve data. In fact, enumeration is the same as iteration. The enumeration name and method name are too long, so it is replaced by the iterator.

ArrayList DEMO code (excluding repeated objects in the Set)

1 public class ArrayListDemo {2 3 public static void main (String [] args) {4 // create an ArrayList type set, add the Person object 5 ArrayList al = new ArrayList (); 6 al. add (new Person ("jack", 12); 7 al. add (new Person ("jack", 12); 8 al. add (new Person ("jack", 11); 9 al. add (new Person ("jack2", 12); 10 al. add (new Person ("jack", 12 ));
// Remove the same object in the al set and print the result 11 System. out. println (singleElement (al); 12} 13 // Delete the repeated element 14 public static ArrayList singleElement (ArrayList arrays) {15 ArrayList al = new ArrayList (); 16 Iterator ite = arrays. iterator (); 17 // Through the while LOOP, in which the contains method of ArrayList is used to determine whether to repeat 18 while (ite. hasNext () {19 Object obj = ite. next (); 20 if (! Al. contains (obj) 21 al. add (obj); 22} 23 return al; 24} 25 26} 27 // Person class 28 class Person {29 private String name; 30 private int age; 31 // a total of 32 public Person (String name, int age) {33 super (); 34 this. name = name; 35 this. age = age; 36} 37 public String getName () {38 return name; 39} 40 public void setName (String name) {41 this. name = name; 42} 43 public int getAge () {44 return age; 45} 46 public void SetAge (int age) {47 this. age = age; 48} 49 // rewrite the equals method to customize the comparison basis. Here we define that the object names and ages are the same, the Object is equal to 50 public boolean equals (Object obj) {51 Person per = (Person) obj; 52 if (! (Obj instanceof Person) 53 return false; 54 else 55 return per. name. equals (name) & per. age = age; 56} 57 // rewrite the toString method to view the program running result 58 public String toString () {59 return name + ":" + age; 60} 61 62}

 

Example code of the consumer list (use consumer list to simulate the stack (first-in-first-out) and queue (first-in-first-out) data structure)

1 public class upload listdemo {2 public static void main (String [] args) {3 4 Queue queue = new Queue (); 5 queue. set ("red"); 6 queue. set ("yellow"); 7 queue. set ("blue"); 8 queue. set ("Purple"); 9 10 while (! Queue. isNull () {11 System. out. println (queue. get (); 12} 13} 14} 15 // simulate the Queue, first-in-first-out 16 class Queue {17 private Queue list ll; 18 public Queue () {19 ll = new jsonlist (); 20} 21 public Object get () {22 return ll. removeFirst (); 23} 24 public void set (Object obj) {25 ll. addLast (obj); 26} 27 public boolean isNull () {28 return ll. isEmpty (); 29} 30} 31 // simulate the Stack-32 class Stack after the first generation {33 private writable list ll; 34 public Stack () {35 ll = new writable list (); 36} 37 public Object get () {38 return ll. removeLast (); 39} 40 public void set (Object obj) {41 ll. addLast (obj); 42} 43 public boolean isNull () {44 return ll. isEmpty (); 45} 46}

 

 

 

 

 

Errors are inevitable for beginners. You are welcome to judge and continue to correct ing ...........

 

ASP. Net + Android + IOS development and Net training. We look forward to communicating with you!

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.