-------<a href= "http://www.itheima.com" target= "blank" >android training </a> <a href= "/http/ Www.itheima.com "target=" blank ">java training </a>, look forward to communicating with you! ----------
I. Concept of a collection
- 1. What is a collection
- A collection is a container that is variable in length and can store objects of any type. The base data type can also be loaded into the collection, but the inside is automatically boxed into the wrapper class object before it is stored in the collection's
Two. Classification of collections
- 1.collection
- list: repeatable, with storage order, indexed
- arraylist array implementation, adding and deleting slow, Find Fast
- linkedlist chain list implementation, delete quickly, find slow
- vector array implementation, The same principle as ArrayList, but thread safe
- set: Non-repeatable, inconsistent access order, no index
- hashset
- treeset
- linkedhashset
- 2.Map
- HashMap
- TreeMap
- Hashtable
- Linkedhashmap
Three. Common methods for collections
- 1. Common methods
- add (Object obj) adds an element to the collection, adds to the last position
- get (int index) gets the element at the specified position in the collection
- size () gets the length of the collection
- add (int index, Object obj) adds an element to the collection, adds it to the specified position
- set ( int index, Object obj replaces the element at the specified position in the collection with the
- remove (int index) Deletes the element at the specified position in the collection
- remove (object obj) deletes the Obj object contained in the collection ( Iterate through the elements in the collection if the incoming element equals an element, call the Remove method, note: The corner label is--)
- 2. Iteration Collection
- A.for Loop: A size ()-1 that loops from 0 to the collection, one for each
- B. Iterator: Call the iterator () method to get the iterator, use Hasnext () to determine if the next element is included, and use Next () to get the next element
- C. Enhanced for Loop: for (type variable name: container) {loop Body} The number of elements in the container executes how many times the loop body is executed, each time the loop variable points to a different element in the container
- 3. Issues that are removed during iteration
- A.for Loop: Delete the loop variable after deletion because the subsequent element will move forward--
- B. iterators: You must use Remove () in iterator to delete an element otherwise an exception will be thrown
- C. Enhanced for loop: Cannot delete
Dark Horse Programmer _java Collection using