Collection、List、Set

1.ArrayList inherited AbstractList while implementing the interface List, and the List interface also inherited the Collection interface, Collection is a collection of top-level interface; Collection is a collection of ancestors so
2.List Interface: how deposit on how to take, there is an array subscript, you can save repetitive elements: common categories: ArrayList class; the LinkedList class;
3.Set Interface: how to do it is not necessarily how to take, no array subscripts, duplicate data not stored; common classes: class HashSet , LinkedHashSet class
4.Collection interfaces is not new polymorphic objects so borrowed subclass object: Collect <generic> variable name = new ArrayList <generic> ();
5.ArrayList <the Person> List = new ArrayList <the Person> (); additive element: list.add (new Person ( "Bob", 12)); new new Person ( "Bob", 12) where added directly Person object, so as to rewrite the constructor direct assignment
6.System.out .println (list.get (i)) If the Person class does not override this method of printing toString address; tostring but a method of rewriting Person then the content in the printing method here
7.Collection <Integer> col = new ArrayList <Integer> (); Collection is because the interface Therefore, new objects can Borrowing subclass ArrayList create objects, polymorphism is
the basic operation of Collection: 1 additional element:. Col.add (123); 2 remove elements:. Col.remove (123);
3. empty set: col.clear; delete the contents in the collection, but also set
8. Collection because the interface is not new to the object so downcast through the collection:
IF (COL instanceof the ArrayList) {the ArrayList <Integer> = ARR (the ArrayList <Integer>) COL; for (int I = 0; I <arr.size (); I ++) {System.out.println (arr.get (I))}
9. the set determines whether there is a elements: Boolean in Flag = col.contains (999);
10. the collection array into: Object arr = col.toArray (); .toArray because the collection () method returns the value of ARR Object Object contact with all
then taken to traverse values: for (Inti = 0; I <arr.length; I ++) {System.out.println (ARR [I])};
11.Collection get the elements in the set is achieved by the iterator: iterator object will iterator first Collection of judgment judged by .hasnext () method returns true if there is no return false;
if .hasNext () returns true then recall .next () method to take out the value in the Collection, iterator The values are determined to a first value; then again determines the value
12.Collection <String> col = new ArrayList <String> (); it.add ( " Hello");
Values need to obtain the iterator object: Iterator <String> it = col.iterator (); col call Collection object retrieval iteartor () method to get iterator object; again assigned to IT; Iterator iterator to generic and collection set as to be traversed; it.hasNext () whether there returns true values are then call it.next () method of determining the set value, then there is no return false end
13. the value of the iterative cycling : while (it.haxnext ()) { system.out.println (it.next ())}; while only inside the loop conditions are true and false to true then the default so it.haxnext () is it.haxnext () == to true
14. A contents using the interpreted character values: while (it.hasnext ()) { String str = " Zha"; if (str.equals ( "Zha")) {system.out.println (str) } Rebels character judgment is then output; and to define certain variables; then determine the output variable; because the collection is the first judgment has no value and then define the value of the variable str received so you can save a live or else output the next value
15.Collection col = new ArrayList (); // if not generic, it can be set to anything stored
col.add (123); // not ADDITIVE The system automatically enhance the value of type object
col.add ( "123");
col.add (123.456);
// Get iterator object
Iterator it = col.iterator (); // set above because it did not write the generic iterator object here is not to write generic
while (it.
Object obj = it.next (); // because the collection did not write the generic Object values are generated; therefore used here to pick Object
IF (the instanceof String obj) {
String S = (String) obj; // turn into characters string
System.out.println (s.length ());
}
}
16. a reinforcement for: dedicated to the set of variables and arrays (no subscript) can not be printed with the collection array subscripts (print value of 1-100); its internal principle iterators Interator, it can not set the element during printing
be altered, but may be modified; without altering the set / length of the array will do.
format: for (element type to traverse (the Person) variable names (p): collection collection or array (arr)) {system.out.println ( " p")};
enhancement for the difference between the old and the for loop: enhanced for loop must have a target, and the target can only a collection of arrays and collections, when traversing then reinforcement if only the array and a set of traverse for
if required to elements of the array and a set of operation then the old-fashioned for loop, by additions and deletions of the target operation change;
static void main 17.public (String [] args) {
COL = new new Collection the ArrayList <the Person> < The Person> ();
col.add (new new the Person ( "Bob", 20)); col.add ( new Person ( " red", 18));
for (Person s: col) {iterator principle is built Interator, first determine whether there col set value (has.next ()) then the values (.next ()) and then take the value assigned to S; is reprinting; then determine, then the value, and then copy, print
system.out.println (s)} and an iterator for enhanced deletions may be modified not only does not change the length of the line set and, if you want to CRUD for the old-fashioned, because enhanced for iterators and the same has been going down can not go back, can not change the length
here is an enhancement for printing custom Person class, the value of col is assigned to the Person type of p objects inside, and then print p because the Person rewrite the toString () method toString so here printed content () method instead of the address inside the
18. traversed by a variety of collection collection method, (wildcard <?>; as generics)
public static void main ( String [] args) {
Collection <the Person> = new new COL the ArrayList <the Person> (); col.add (new new the Person ( "Bob", 20); col.add (new Studemt ( " red", 19));
Collection <Student> col2 = new rrayList <Student> (); col.add (new Student ( " black", 10)); col.add ( new Student ( " small blue" , 20));
Calling the same get () method traversing: get (col); get ( col2);}
public static void get (Collection <? > col) {// (Collection <?> col) of? is a wildcard, in the generics do not know what is, when there? instead of the generic so you can call a variety of collection pan collection type
Interator it = col.iterator ();? has been strong for the collection can not be traversed with wildcards, because do not know for enhanced wildcards;? can only be traversed with iterators
while (it.hasnext ()) {system . Out.println (it.next ());}
public static void main (String [] args) {
Collection <Cook> = new new col1 the ArrayList <Cook> ();
col1.add (new new Cook ());
Collection <Waiter > = new new col2 the ArrayList <Waiter> ();
col2.add (new new Waiter ());
collection <Scanner> = new new col3 the ArrayList <Scanner> ();
// a modulation method through the collection
GET (col1);
GET (col2 );
// GET (col3);
}
<? extends Emp> public static void GET (Collection COL) {indicate that extends must be a subclass inherits Emp Emp and to pass over; super mean Emp is the father of the bottom subclass,Emp only parent who (parent, grandfather class) and to pass over its own Emp
<?> Iterator it = col.iterator ( ); // Collection <extends Emp?> Here is the mass subclass inherits Emp and Emp; is meant an upper limit; upper limit Emp
the while (it.hasNext ()) { // Collection transmitted here is the parent Emp and Emp <super Emp?>; means that the lower limit; lower limit Emp
System.out.println (it.next ()); // not provided because the upper and lower wildcards ? what classes exist so pass line, has been restricted so that
}
}
characteristic 19.List: there are subscripts, how to take out how stored, can be stored duplicate value; the user can find the exact set of elements in accordance with the insertion position related elements, and operation of the memory elements by the index into
subclasses 20.List interface: ArrayList (multiple queries) and the LinkedList (storing more);
21. the method inherited from an indexed Collection. List is unique method of
storing the characteristics of its subclasses 22.List: stack: 1. FILO (after it is stored into the first set are sequentially extracted in the extraction, until they are removed after the end of extraction)
inlet and outlet are push stack tip position; push: the element is stored; popping: element is removed
queue: 1. FIFO (memory element to go before removal element in front of it sequentially extracts); 2. an inlet, an outlet side half and
array collection (ArrayList) 23. Array type: fast lookup element 1 (by. index to quickly find the element at the location; because there subscript so the query faster)
add or delete elements slow: because of the need to create a new array when its length can not change, add or delete an element, and then add or remove post stick into the entire array
Array set list type (LinkedList): a pointer value field + domain (the address of the next memory element) composition; address by a link between a plurality of element node
1. Find element slow (because there is no subscript, prepared by the address one by one to find); want to find an element, you need to connect the node, and then click Find specified element backward
2. fast speed deletions: because add or delete an element directly change the next address link element on the line;
24.ArrayList is collection array type: query speed, slow additions, the multi-purpose set ArrayList often need to query the data storage; query for multiple services.
, 25.LinkedList is a collection of linked list types: fast additions and deletions speed, slow queries, so much store business use LinkedList
26.5.6 the Vector collection; that is, early ArrayList (same method) (ArrayList and arrays are the same type); enumeration is now iterator (same method)
27.List:
public static void main (String [] args) {
List <String> = new List the ArrayList <String> (); List because the interface is not directly subject in need of new polymorphism means, means for new ArrayList subclass objects
List.add ( "a");
List.add (0, "B"); the element is inserted in the specified location, the location of the insertion element at index 0 B
List.add ( " c "); traversal: for (int i = 0; i <list.size (); i ++) {system.out.println (list.get (i)}; List because with the indices may be made for circulating old The value
Delete element at the location, where str is the return value, is to return back to delete an element, that value is to delete str
String str = list.remove (2); return value because there is a generic, pan and List type is String, String str will be used here to pick, .remove (subscript), where the subscripts will come to remove water filled
modify the value at the specified location: list.set (0, "Hello by subscripts // modifying the value of the element; and b changed Hello ")
traverse; because there are not only the value of the length values deletions should not change the set operation, it may be enhanced for
for (S String: List) {System.out .println (s)}

28.LinkedList: public static void main (String [] args) {
the LinkedList <String> new new ARR = the LinkedList <String> ();
ARR, addFrist ( "A"); .addFrist () is added as the first element meaning elements, but before the subsequent call .addFrist elements () method will be replaced with the first element or existing
arr.addFrist ( "B");
arr.addLast ( "C"); .addLast () to mean the additive element is the last element, but will be before the subsequent replacement element .addLast call () method or a conventional last element
arr.addFrist ( "D");
for (String S: ARR) {System .out.println (s)} // results d, b, a, c
out of a first position of the element
arr.removeFrist () remove a first element position
to obtain the last element .getLast (): system.out. the println (arr.getLast ());
// determines whether elements in the set: system.out.println (arr.isEmpty ()); the result is false, isEmpty () is to determine whether the set is empty no value, it is If no value is false returns true

Guess you like

Origin www.cnblogs.com/hankai2735/p/11331861.html