Set 1 -- video Study Notes for bixiangdong java basic tutorial; 1 -- bixiangdong

Source: Internet
Author: User

Set 1 -- video Study Notes for bixiangdong java basic tutorial; 1 -- bixiangdong

Day14 Collection framework
01 System Overview
02 common methods
03 iterator
04 common methods of List sets
05 ListIterator
06 features of specific objects in a List set
Enumeration in 07 Vector

 

01 System Overview

Collection class
Why does the collection class appear?
Object-oriented language (OSS) is used to display objects. Therefore, objects must be stored to facilitate operations on multiple objects.
A set is the most common way to store objects.

What is the difference between arrays and collection classes that are containers?
Although the array can also store objects, the length is fixed, and the length of the set is variable.
The array can be stored in the current data type, and the set can only store objects.
Collection class features:
A set is only used to store objects. The length of a set is variable. A set can store different types of objects.


02 common methods

1. Parameters of the add method are of the object type, so as to accept any type of objects.
2. All objects stored in the collection are referenced (addresses) of objects ).

1 import java. util. *; 2 class CollectionDemo 3 {4 public static void main (String [] args) 5 {6 method (); 7 base_Method (); 8 9 10} 11 // calculates the intersection 12 public static void method () 13 {14 ArrayList al = new ArrayList (); 15 al. add ("java01"); 16 al. add ("java02"); 17 al. add ("java03"); 18 al. add ("java04"); 19 sop ("al original set" + al); 20 21 ArrayList al = new ArrayList (); 22 al2.add ("java01 "); 23 al2.add ("java07"); 24 al2.add ("java03"); 25 al2.add ("java08"); 26 sop ("al 2 original set" + al); 27 28 al. retainAll (al); 29 sop ("set after al is submitted" + al); 30 31 32} 33 public static void base_Method () 34 {35 // create a Collection container and use the Collection interface subclass. ArrayList36 ArrayList al = new ArrayList (); 37 38 39 // 1. add element 40 al. add ("java01"); 41 al. add ("java02"); 42 al. add ("java03"); 43 al. add ("java04"); 44 45 // print the set 46 sop ("original set:" + al); 47 // 2. delete element 48 al. remove ("java02"); 49 sop ("changed set:" + al); 50 // al. clear (); 51 52 // 4. determine element 53 sop ("java03 exists" + al. contains ("java03"); 54 sop ("whether the set is empty" + al. isEmpty (); 55 56 // obtain the set length 57 sop ("size:" + al. size (); 58} 59 public static void sop (Object obj) 60 {61 System. out. println (obj); 62} 63}View Code

 

03 iterator

What is an iterator?
It is actually the way to retrieve elements from the collection.

Define the fetch method within the set.
In this way, you can directly access the elements in the set.
The fetch method is defined as an internal class.
The data structure of each container is different,
The extracted action details are different, but they all share the same content.
You can determine and retrieve commonalities.

These internal classes all comply with one rule. This rule is Iterator.
How can I obtain the object retrieved from the set?
Using an external Method
Iterator ();

1 import java. util. *; 2 class CollectionDemo 3 {4 public static void main (String [] args) 5 {6 method (); 7 8} 9 // calculates the intersection 10 public static void method () 11 {12 ArrayList al = new ArrayList (); 13 al. add ("java01"); 14 al. add ("java02"); 15 al. add ("java03"); 16 al. add ("java04"); 17 18 // get Iterator, used to retrieve element 19/* Iterator it = al in the set. iterator (); 20 21 while (it. hasNext () 22 {23 sop (it. next (); 24} 25 */26 for (Iterator it = al. iterator (); it. hasNext ();) 27 {28 sop (it. next (); 29} 30 31} 32 33 public static void sop (Object obj) 34 {35 System. out. println (obj); 36} 37}View Code

 


04 common methods of List sets

Collection
| -- List: the elements are ordered and can be repeated. This set has an index.
| -- Set: the elements are unordered and cannot be repeated.

List:
Any method that can operate the badge is a unique method of the system.

Add (index, element)
AddAll (index, Collection)

Delete: remove (index)

Change: set (index, element)

Query: get (index)
SubList (from,)
ListIterator ();

1 import java. util. *; 2 class ListDemo 3 {4 public static void sop (Object obj) 5 {6 System. out. println (obj); 7} 8 public static void main (String [] args) 9 {10 ArrayList al = new ArrayList (); 11 al. add ("java01"); 12 al. add ("java02"); 13 al. add ("java03"); 14 sop ("original set:" + al); 15 16 // add element 17 al at specified position. add (1, "java09"); 18 sop ("changed collection:" + al); 19 20 // Delete element 21 at the specified position // al. remove (2); 22 23 // modify the element 24 al. set (2, "java07"); 25 // sop (al); 26 27 // obtain element 28 sop ("get (I)" + al. get (1); 29 30 // get all elements 31 for (int x = 0; x <al. size (); x ++) 32 {33 System. out. println ("al (" + x + ") =" + al. get (x); 34} 35 36 37 for (Iterator it = al. iterator (); it. hasNext ();) 38 {39 sop ("next:" + it. next (); 40} 41 42} 43}View Code

 

05 ListIterator

ListIterator is a sub-interface of Iterator.
During iteration, elements in the set cannot be operated through the set method.
ConcurrentModificationException occurs.

Therefore, during iteration, only the method of the iterator can be used to operate elements, but the iterator method is limited.
Only elements can be judged, retrieved, and deleted.
If you want to perform other operations, such as adding or modifying, you need to use its subinterface ListIterator.

This interface can only be obtained through the ListIterator method of List.

1 import java. util. *; 2 class ListDemo 3 {4 public static void sop (Object obj) 5 {6 System. out. println (obj); 7} 8 public static void main (String [] args) 9 {10 ArrayList al = new ArrayList (); 11 al. add ("java01"); 12 al. add ("java02"); 13 al. add ("java03"); 14 sop ("original set:" + al); 15 16 ListIterator li = al. listIterator (); 17 // during iteration, the Operation element 18 while (li. hasNext () 19 {20 Object obj = li. next (); 21 if (obj. equals ("java02") 22 li. set ("java006"); 23} 24 sop ("changed collection:" + al); 25 26 // output in reverse order 27 while (li. hasPrevious () 28 {29 sop ("pre:" + li. previous (); 30 31} 32 sop (al); 33 34} 35}View Code

 


06 features of specific objects in a List set

| -- List: the elements are ordered and can be repeated. This set has an index.
| -- ArrayList: array structure used by the underlying data structure. Feature: the query speed is fast, but the addition and deletion are slow. The more elements, the more obvious the query speed. The thread is not synchronized.
| -- Linked list: the bottom layer uses the Linked List data structure. Features: Fast addition, deletion, and query speed.
| -- Vector: The underlying layer is the array data structure. It appears first than ArrayList. Thread synchronization. Replaced by ArrayList.


Enumeration in 07 Vector

1/* 2 enumeration is a unique way to retrieve a Vector. 3. We found that enumeration is very similar to iterator. 4. In fact, enumeration is the same as iteration. 5 6 because the enumeration name and method name are too long. 7. Therefore, it is replaced by the iterator. 8. enumeration is gloomy. 9 */10 import java. util. *; 11 12 class VectorDemo 13 {14 public static void main (String [] args) 15 {16 Vector v = new Vector (); 17 18 v. add ("java01"); 19 v. add ("java02"); 20 v. add ("java03"); 21 v. add ("java04"); 22 23 Enumeration en = v. elements (); 24 25 while (en. hasMoreElements () 26 {27 System. out. println (en. nextElement (); 28} 29 30} 31}

 

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.