1: Object Array (Master)
(1) Arrays can store either the base data type or the reference type. It is called an array of objects when it stores reference types.
2: Set (Collection) (master)
(1) The origin of the collection?
We are learning Java-object-oriented-manipulating many objects--storage--containers (arrays and StringBuffer)--arrays
While the length of the array is fixed, so it is not suitable for changing requirements, Java provides a collection for us to use.
(2) What is the difference between a set and an array?
A: Length Difference
Array pinning
Set Variable
B: Content Differences
An array can be a base type, or it can be a reference type
Collections can only be reference types
C: element content
Arrays can only store the same type
Collections can store different types (in fact, collections typically store the same type)
(3) The inheritance architecture of the collection?
Because of the different requirements, Java provides different collection classes. The data structure of the multiple collection classes is different, but they are all to provide storage and traversal functionality,
We extract the generality of them continuously, and finally form a set of inheritance architecture diagram.
Collection
|--list
|--arraylist
|--vector
|--linkedlist
|--set
|--hashset
|--treeset
(4) Collection Function Overview (self-completion)
A: Add Function: Boolean Add (e) To ensure that this collection contains the specified element (optional action).
B: Remove Function: Boolean remove (Object o) removes a single instance of the specified element from this collection, if one exists (optional action).
C: Judging function:
Boolean IsEmpty () returns True if this collection does not contain an element.
Boolean contains (Object o) returns True if this collection contains the specified element
D: Gets the function:iterator<e> Iterator () returns an iterator that iterates over the elements of this collection.
E: Length function: int size () returns the number of elements in this collection.
F: Intersection (Understanding): Boolean Retainall (collection<?> C) retains only those elements in this Collection that are also included in the specified Collection (optional operation).
G: Turn the set into an array (understand): object[] ToArray () returns an array containing all the elements in this collection.
(5) Traversal of the collection collection
A: Turn the set into an array (learn)
B: Iterator (Collection-specific method)
(6) iterators
A: Is the way the collection gets the elements.
B: It is dependent on the collection and exists.
C: The principle and source of the iterator.
A: Why is the definition for an interface instead of an implementation class?
B: Look at the inner class implementation of the iterator.
(7) Case of collection collection (traversal mode iterator)
Procedure for collection:
A: Create A Collection Object
B: Create an Element object
C: Add elements to the collection
D: Traversing the collection
A: Store Strings and traverse
Import java.util.Collection;
Import java.util.ArrayList;
Import Java.util.Iterator;
public class Collectiondemo {
public static void Main (string[] args) {
To create a collection object
Collection C = new ArrayList ();
Creating and adding elements
C.add ("Hello");
C.add ("World");
C.add ("Java");
Iterating through the collection
Iterator it = C.iterator ();
while (It.hasnext ()) {
string s = (string) it.next ();
System.out.println (s);
}
}
}
B: Store custom objects and traverse
public class Student {
private String name;
private int age;
Public Student () {}
Public Student (String Name,int age) {
THIS.name = name;
This.age = age;
}
GetXxx ()/setxxx ()
}
Import java.util.Collection;
Import java.util.ArrayList;
Import Java.util.Iterator;
public class Studentdemo {
public static void Main (string[] args) {
To create a collection object
Collection C = new ArrayList ();
Create Student objects
Student S1 = new Student ("Brigitte", 27);
Student s2 = new Student ("Wind Qingyang", 30);
Student s3 = new Student ("Elina", 30);
Student S4 = new Student ("Wu Xin", 25);
Student S5 = new Student ("Lynn Qu", 16);
adding elements
C.add (S1);
C.add (S2);
C.add (S3);
C.add (S4);
C.add (S5);
Iterating through the collection
Iterator it = C.iterator ();
while (It.hasnext ()) {
Student s = (Student) it.next ();
System.out.println (S.getname () + "---" +s.getage ());
}
}
}
3: Collection (List) (master)
(1) List is a sub-interface of collection
Features: Orderly (the storage sequence and the order of removal), repeatable.
(2) Unique features of list: (self-completion)
A: Add function: void add (int index,e Element) inserts the specified element (optional action) at the specified position in the list.
B: Remove function: E remove (int index) removes the element (optional action) from the specified position in the list.
C: Get function: E get (int index) returns the element at the specified position in the list.
D: iterator function:listiterator<e> Listiterator () returns the list iterator (in the appropriate order) for this list element. (The list collection-specific iterator, bidirectional iterator, common iterator iterator is unidirectional.) and must be traversed before you can traverse backwards)
E: Modify function: E set (int index,e element) replaces the element (optional action) of the specified position in the list with the specified element.
(3) Unique traversal capabilities of the list collection
A: Combined by size () and get ().
B: Code Demo
To create a collection object
List List = new ArrayList ();
Creating and adding elements
List.add ("Hello");
List.add ("World");
List.add ("Java");
Iterating through the collection
Iterator it = List.iterator ();
while (It.hasnext ()) {
string s = (string) it.next ();
System.out.println (s);
}
System.out.println ("----------");
for (int x=0; x<list.size (); x + +) {
string s = (string) list.get (x);
System.out.println (s);
}
(4) The unique function of the list iterator; (understanding)
It can be traversed backwards, but it must be traversed first, so it is meaningless and not used.
(5) Concurrency modification exceptions(so the collection cannot be modified by the collection object when iterating through the collection)
A: a phenomenon that appears
Iterators iterate through collections, collections modify collection elements
B: Reason
Iterators are dependent on collections, and the collection's change iterators are not known.
C: Solutions
A: Iterator traversal, iterator modification (listiterator), is the use of iterators to modify the collection elements.
Element is added at the location of the iteration just
B: Collection traversal, collection modification (size () and get ())
Element is added at the end of the collection
(6) Common data structures
A: Stack advanced back out
B: Queue FIFO
C: Array query fast, adding and deleting slow
D: List query slow, and delete fast
(7) Sub-class features of list (interview questions)
ArrayList
The underlying data structure is an array, query fast, and delete slowly.
Threads are unsafe and highly efficient.
Vector
The underlying data structure is an array, query fast, and delete slowly.
Thread-safe, low-efficiency.
LinkedList
The underlying data structure is linked list, query slow, delete quickly.
Threads are unsafe and highly efficient.
Who do you use? Look at the demand?
Analysis:
Are you safe?
To: Vector (even if you want to, do not use this, later on)
Do not: ArrayList or LinkedList
query more; ArrayList
More additions and deletions: LinkedList
Don't know anything, just use ArrayList.
(8) Case of List collection (traversal mode iterator and normal for)
A: Store Strings and traverse
B: Store custom objects and traverse
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The collection collection in Java and the corresponding subclass list class