15.01 Set Framework (Overview and Use of Object Arrays)
- A: Case demonstration
- Requirement: I have five students. Please store the information of these five students in the array and traverse the array to get the information of each student.
Student[] arr = new Student[5]; //Storing Student Objects arr[0] = new Student("Zhang San", 23); arr[1] = new Student("Li Si", 24); arr[2] = new Student("Wang Wu", 25); arr[3] = new Student("Zhao Liu", 26); arr[4] = new Student("Margo", 20); for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); }
-
B: Drawing demonstration
- Explain the case drawing of the student array
- Arrays and collections store reference data types, all with address values
15.02 Set Framework (Origin of Sets and Set Inheritance System Diagram)
- A: Origin of Collection
- The length of the array is fixed. When the added elements exceed the length of the array, we need to redefine the array. It's too troublesome. java provides us with collection classes, which can store arbitrary objects. The length can be changed. It increases with the increase of the elements and decreases with the decrease of the elements.
- B: The difference between arrays and sets
- The difference is 1:
- Arrays can store both basic data types and reference data types. Basic data types store values and reference data types store address values.
- Collections can only store reference data types (objects) or basic data types in collections, but they are automatically boxed into objects when stored.
- Distinguish 2:
- Array length is fixed and cannot grow automatically
- The length of the collection is variable and can grow with the increase of elements.
- The difference is 1:
- C: When do arrays and collections work?
* 1. If the number of elements is a fixed recommended array
* 2. If the number of elements is not a fixed recommended set - D: Set Inheritance System Diagram
15.03 Collection Framework (Basic Functional Testing of Collection Sets)
- A: Case demonstration
Demonstration of Basic Functions boolean add(E e) boolean remove(Object o) void clear() boolean contains(Object o) boolean isEmpty() int size()
B: note:
-
collectionXxx.java uses unchecked or unsafe operations.
Note: For more information, use - Xlint:unchecked to recompile.
The java compiler considers the program to be a security hazard
Warm Tip: This is not a compilation failure, so don't pay attention to it until you learn generics.
15.04 Set Framework (Set Turn Array Traversal for Set Traversal)
- A: Traversal of Sets
- In fact, each element in the collection is acquired in turn.
-
B: Case demonstration
- Converting a collection into an array enables traversal of the collection
-
toArray()
*Collection coll = new ArrayList(); coll.add(new Student("Zhang San",23)); //Object obj = new Student("Zhang San", 23); coll.add(new Student("Li Si",24)); coll.add(new Student("Wang Wu",25)); coll.add(new Student("Zhao Liu",26)); Object[] arr = coll.toArray(); //Converting a collection into an array for (int i = 0; i < arr.length; i++) { Student s = (Student)arr[i]; //Strong to Student System.out.println(s.getName() + "," + s.getAge()); }
15.05 Collection Framework (Collection Set with All Functional Testing)
- A: Case demonstration
Functional Demonstration with All boolean addAll(Collection c) boolean removeAll(Collection c) boolean containsAll(Collection c) boolean retainAll(Collection c)
package com.heima.collection;
import java.util.ArrayList;
import java.util.Collection;
@SuppressWarnings({ "rawtypes", "unchecked" })
public class Demo4_CollectionAll {
/**
* * A:Case demonstration
*
Functional Demonstration with All
boolean addAll(Collection c)
boolean removeAll(Collection c)
boolean containsAll(Collection c)
boolean retainAll(Collection c)
*/
public static void main(String[] args) {
//demo1();
//demo2();
//demo3();
Collection c1 = new ArrayList();
c1.add("a");
c1.add("b");
c1.add("c");
c1.add("d");
Collection c2 = new ArrayList();
c2.add("a");
c2.add("b");
c2.add("c");
c2.add("d");
c2.add("e");
c2.add("f");
//Take the intersection, return true if the collection of calls changes, and false if the collection of calls remains unchanged
boolean b = c1.retainAll(c2); //intersect
System.out.println(b);
System.out.println(c1);
}
public static void demo3() {
Collection c1 = new ArrayList();
c1.add("a");
c1.add("b");
c1.add("c");
c1.add("d");
Collection c2 = new ArrayList();
c2.add("a");
c2.add("b");
c2.add("z");
boolean b = c1.containsAll(c2); //Determine whether the collection invoked contains the incoming collection
System.out.println(b);
}
public static void demo2() {
Collection c1 = new ArrayList();
c1.add("a");
c1.add("b");
c1.add("c");
c1.add("d");
Collection c2 = new ArrayList();
c2.add("a");
c2.add("b");
c2.add("z");
boolean b = c1.removeAll(c2); //Delete intersection
System.out.println(b);
System.out.println(c1);
}
public static void demo1() {
Collection c1 = new ArrayList();
c1.add("a");
c1.add("b");
c1.add("c");
c1.add("d");
Collection c2 = new ArrayList(); //alt + shift + r renamed
c2.add("a");
c2.add("b");
c2.add("c");
c2.add("d");
//c1.addAll(c2); // Add every element in C2 to c1
c1.add(c2); //Add c2 as an object to c1
System.out.println(c1);
}
}
15.06 Set Framework (iterator traversal of set traversal)
- A: Overview of Iterators
- Collections are used to store elements, stored elements need to be viewed, then iteration (traversal)
-
B: Case demonstration
-
Use of iterators
Collection c = new ArrayList(); c.add("a"); c.add("b"); c.add("c"); c.add("d"); Iterator it = c.iterator(); //Get a reference to the iterator while(it.hasNext()) { //Iterative methods in sets (traversal) System.out.println(it.next()); }
-
15.07_Collection Framework (Collection stores custom objects and traverses)
-
A: Case demonstration
- Collection stores custom objects and traverses them with iterators
Collection c = new ArrayList(); c.add(new Student("Zhang San",23)); c.add(new Student("Li Si",24)); c.add(new Student("Wang Wu",25)); c.add(new Student("Zhao Liu",26)); c.add(new Student("Zhao Liu",26)); for(Iterator it = c.iterator();it.hasNext();) { Student s = (Student)it.next(); //Downward transformation System.out.println(s.getName() + "," + s.getAge()); //Get the name and age in the object } System.out.println("------------------------------"); Iterator it = c.iterator(); //Obtain iterators while(it.hasNext()) { //Determine whether there are elements in a set //System.out.println(((Student)(it.next())).getName() + "," + ((Student)(it.next())).getAge()); Student s = (Student)it.next(); //Downward transformation System.out.println(s.getName() + "," + s.getAge()); //Get the name and age in the object }
15.08 Set Framework (Principle of Iterator and Source Code Analysis) (Understanding)
- A: Iterator Principle
- Iterator Principle: Iterators traverse collections, and the internal storage structure of each collection is different, so each collection storage and extraction is different, then we need to define hasNext() and next() methods in each class. This is possible, but it will make the whole collection system too bulky. Iterators will extract the interface from such methods upwards, and then in each class, we need to define hasNext() and next() methods. There are two advantages of defining its own iteration method inside the class. First, it stipulates that the whole set system is traversed by hasNext() and next() methods. Second, the code is implemented internally at the bottom. Users can use it regardless of how they implement it.
- B: Iterator Source Parsing
- 1. Find the ArrayList class in eclipse ctrl + shift + t
- 2. Ctrl + O lookup iterator() method
- 3. See that the return value type is new Itr(), indicating that the class Itr implements the Iterator interface.
- 4. Find the internal class Itr and find that all abstract methods in Iterator have been overwritten
15.09 Set Framework (Overview and Testing of the Specific Functions of List Sets)
- A: Overview of the Special Functions of List Collections
- void add(int index,E element)
- E remove(int index)
- E get(int index)
- E set(int index,E element)
15.10_Collection Framework (List Collection stores student objects and traverses)
-
A: Case demonstration
-
Traversal is used by combining size() and get() methods.
List list = new ArrayList(); list.add(new Student("Zhang San", 18)); list.add(new Student("Li Si", 18)); list.add(new Student("Wang Wu", 18)); list.add(new Student("Zhao Liu", 18)); for(int i = 0; i < list.size(); i++) { Student s = (Student)list.get(i); System.out.println(s.getName() + "," + s.getAge()); }
-
15.11_ Set Framework (Causes and Solutions for Concurrent Modification Exceptions)
-
A: Case demonstration
-
Requirement: I have a collection, excuse me, I want to determine whether there is a "world" element in it, if there is, I will add a "javaee" element, please write code implementation.
List list = new ArrayList(); list.add("a"); list.add("b"); list.add("world"); list.add("d"); list.add("e"); /*Iterator it = list.iterator(); while(it.hasNext()) { String str = (String)it.next(); if(str.equals("world")) { list.add("javaee"); //Concurrent ModificationException is thrown here and concurrently modified exceptions are thrown. } }*/
-
-
B: Concurrent ModificationException appears
- Iterator traversal, set modification set
-
C: Solutions
- a: Iterator iteration element, iterator modification element (ListIterator's special function add)
-
b: Set traversal elements, set modification elements
ListIterator lit = list.listIterator(); //If you want to add elements during traversal, you can use the add method in ListIterator while(lit.hasNext()) { String str = (String)lit.next(); if(str.equals("world")) { lit.add("javaee"); //list.add("javaee"); } }
15.12 List Iterator (Understanding)
- Whether boolean hasNext() has the next one
- Whether boolean hasPrevious() has the previous one
- Object next() returns the next element
- Object previous(); returns the previous element
15.13 Set Framework (Vector's Special Functions) (Understanding)
- A: Overview of Vector Classes
- B:Vector class-specific functions
- public void addElement(E obj)
- public E elementAt(int index)
- public Enumeration elements()
-
C: Case demonstration
-
Iteration of Vector
Vector v = new Vector(); //Create a collection object, a subclass of List v.addElement("a"); v.addElement("b"); v.addElement("c"); v.addElement("d"); //Vector iteration Enumeration en = v.elements(); //Get enumeration while(en.hasMoreElements()) { //Determine whether there are elements in a set System.out.println(en.nextElement());//Get the elements in the collection }
-
15.14 Set Framework (Array and Link List of Data Structures)
- A: array
- Quick query and quick revision
- Add or delete slowly
- B: linked list
- Slow queries and slow revisions
- Additions and deletions quickly
15.15_ Set Framework (Features of the Three Subclasses of List)
- A: The Characteristics of Three Subclasses of List
ArrayList: The underlying data structure is arrays, which make queries fast and add or delete slowly. Threads are insecure and efficient. Vector: The underlying data structure is arrays, which make queries fast and add or delete slowly. Threads are safe and inefficient. Vector is slower than ArrayList queries (thread-safe) Vector adds or deletes slowly relative to LinkedList (array structure) LinkedList: The underlying data structure is linked list, which is slow to query and fast to add or delete. Threads are insecure and efficient. The difference between Vector and Array List Vector is thread-safe and inefficient ArrayList is thread insecure and efficient Common: All are implemented by arrays The difference between ArrayList and LinkedList The bottom layer of ArrayList is the array result, which can be queried and modified quickly. LinkedList is linked list structure at the bottom, adding and deleting faster, querying and modifying slower. Common: Thread insecurity
- B:List has three sons. Who are we using?
Query Multipurpose ArrayList
Add and delete more LinkedList
If there are more ArrayList s