Collection framework,
I. Overview of object arrays
Array and set storage reference data types, all stored are address values
Ii. Collection origin and collection inheritance system diagram
1. The origin of the Set
The array length is fixed. when the length of the added element exceeds the length of the array, you need to re-define the array, which is too troublesome. java provides a collection class for us to store arbitrary objects, the length can be changed. The length increases with the increase of elements and decreases with the decrease of elements.
2. Differences between arrays and collections
Difference 1:
Arrays can store both basic data types and reference data types. Basic data types store values and Reference Data Types store address values.
A collection can only store referenced data types (objects) and can also store basic data types. However, it is automatically packed into objects during storage.
Difference 2:
The array length is fixed and cannot be automatically increased.
The length of a set is variable and can increase according to the increase of elements.
3. When to use arrays and collections?
If the number of elements is fixed, an array is recommended.
If the number of elements is not a fixed recommendation set
4. Collection inheritance system diagram
Both list and set Interfaces
Iii. Test the basic functions of the Collection set
1. Basic functions
Boolean add (E e)
The add method returns true if it is a List Set, because duplicate elements can be stored in the List Set. If it is a Set, false is returned when duplicate elements are stored.
Boolean remove (Object o)
1 System. out. println (colle. remove (1); // delete a specified element. You can delete the basic data type and automatically bind it to. 2 System. out. println (colle );
Void clear ()
Boolean contains (Object o)
Boolean isEmpty ()
Int size ()
2. Note:
CollectionXxx. java uses unchecked or insecure operations.
Note: For more information, use-Xlint: unchecked to recompile.
Java compiler considers this program to have security risks
Tip: this is not a compilation failure, so you don't need to worry about it. You will know it when you learn the generic type.
Iv. traversal of a set-to-array Traversal
Traversal of a set-to-array Traversal
1. traversal of a set
In fact, each element in the set is obtained in sequence.
2. Convert the set into an array to traverse the set.
V. Test the All function of the Collection set
1. boolean addAll (Collection c)
Add each element in c to the specified reference, and add (Collection c) directly adds c as an element to the specified reference.
2. boolean removeAll (Collection c)
The deleted result is an intersection.
3. boolean containsAll (Collection c)
Determines whether the called set contains the imported set.
4. boolean retainAll (Collection c)
Returns true if the called set changes. Otherwise, returns false.
6. Set traversal: iterator Traversal
1. Use of iterator
1 List list = new ArrayList (); 2 Student stu; 3 list. add (new Student ("James", 23); 4 list. add (new Student ("Li Si", 24); 5 list. add (new Student ("Wang Wu", 25); 6 list. add (new Student ("Zhao ", 26); 7 Iterator <Student> iter = list. iterator (); // gets the iterator reference 8 while (iter. hasNext () {// iteration method (traversal) in the set 9 System. out. println (iter. next (); 10}
The premise is to rewrite the toString method in Student; otherwise, the toString method in the Object class will be called.
2. The Collection stores custom objects and traverses them.
1 Collection coll = new ArrayList (); 2 coll. add (new Student ("James", 23); 3 coll. add (new Student ("Li Si", 24); 4 coll. add (new Student ("Wang Wu", 25); 5 Iterator iter = coll. iterator (); // get iterator 6 while (iter. hasNext () {// determines whether an element exists in the set. 7 Student stu = (Student) iter. next (); // perform a downward transformation of 8 System. out. println (stu. getName () + "..... "+ stu. getAge (); 9}7. Principles and source code analysis of the iterator
1. iterator Principle
Iterator Principle: The iterator traverses the set, and the storage structure of each set is different, so each set is saved and retrieved differently, then you need to define the hasNext () and next () methods in each class. This is acceptable, but it will make the entire collection system too bloated, the iterator extracts the interface from this method and defines its own iteration method within each class. There are two advantages to doing so, the first rule sets that the traversal methods of the entire collection system are hasNext () and next () methods. Second, the Code has the underlying internal implementation, and the user does not have to worry about how to implement it.
2. iterator source code parsing
In eclipse, ctrl + shift + t find the ArrayList class
Ctrl + o find iterator () method
Check that the returned value type is new Itr (). It indicates that the Itr class implements the Iterator interface.
Find the Itr internal class and find that all the abstract methods in Iterator are overwritten.
8. Overview and test of specific functions of the List set
1. Overview of specific functions of the List set
Void add (int index, E element)
E remove (int index)
E get (int index)
E set (int index, E element)
9: the List set stores student objects and traverses them.
1 List list = new ArrayList (); 2 Student stu; 3 list. add (new Student ("James", 23); 4 list. add (new Student ("Li Si", 24); 5 list. add (new Student ("Wang Wu", 25); 6 list. add (new Student ("Zhao ", 26); 7 Iterator <Student> iter = list. iterator (); // gets the iterator reference 8 while (iter. hasNext () {// iteration method (traversal) in the set 9 System. out. println (iter. next (); 10} 11 12 for (int I = 0; I <list. size (); I ++) {13 stu = (Student) list. get (I); 14 System. out. println (stu. getName () + "... "+ stu. getAge (); 15}10: causes and solutions for concurrent modification exceptions
1. Requirement: I have a set. Could you tell me if there is any "hello" element in it? If there is one, I will add a "world" element. Please write the code for implementation.
1 List <String> list = new ArrayList <String> (); 2 list. add ("a"); 3 list. add ("B"); 4 list. add ("hello"); 5 list. add ("c"); 6 list. add ("d"); 7 Iterator <String> iter = list. iterator (); 8 String str; 9 while (iter. hasNext () {10 str = (String) iter. next (); 11 if ("hello ". equals (str) {12 list. add ("world"); // ConcurrentModificationException will be thrown here for concurrent modification exception 13} 14} 15 System. out. println (list );
2. ConcurrentModificationException
Iterator traversal, set modification set
3. Solution
A: iterator iteration element, iterator modification element (ListIterator's special function add)
B: Set traversal elements, set modification Elements
1 List <String> list = new ArrayList <String> (); 2 list. add ("a"); 3 list. add ("B"); 4 list. add ("hello"); 5 list. add ("c"); 6 list. add ("d"); 7 ListIterator <String> iter = list. listIterator (); // you can use ListIterator 8 while (iter. hasNext () {9 String str = (String) iter. next (); 10 if ("hello ". equals (str) {11 iter. add ("world"); 12} 13} 14 System. out. println (list );11: ListIterator
1. boolean hasNext () whether there is a next
2. boolean hasPrevious () whether the previous one exists
3. Object next () returns the next element.
4. Object previous (); returns the previous Element
12: unique functions of Vector
1. Special Functions of the Vector class
Public void addElement (E obj)
Public E elementAt (int index)
Public Enumeration elements ()
2. iteration of Vector
1 Vector <String> vector = new Vector <String> (); // creates a collection object, subclass of List 2 vector. add ("a"); 3 vector. add ("B"); 4 vector. add ("hello"); 5 vector. add ("c"); 6 vector. add ("d"); 7 Enumeration <String> enu = vector. elements (); // obtain the enumerated 8 while (enu. hasMoreElements () {// checks whether the collection contains Element 9 System. out. println (enu. nextElement (); // obtain element 10 from the set}