Java Learning Diary-11 Set (1)

Source: Internet
Author: User

Collection interface
The collection stores only the reference to the object, not the object itself.

1.arraylist<e> class
The relationship between 1.1ArrayList and collection
public interface List<e>extends collection<e>
public class Arraylist<e> implements List<e>

1.2 Construction methods
ArrayList () Constructs an empty list with an initial capacity of 10.
ArrayList (collection<? extends e> c) constructs a list of elements that contain the specified Collection, which are in accordance with the
The collection iterators return them in the order in which they are arranged.
ArrayList (int initialcapacity) constructs an empty list with the specified initial capacity.

1.3 Basic methods
Boolean Add (E) adds the specified element to the tail of the list
Boolean AddAll (collection<? extends e> c) adds all elements from another collection to ArrayList, allowing duplicates
Elements
public boolean RemoveAll (collection<?> c) complement set A-B (inherited from Java.util.AbstractCollection)
void Add (int index, E Element) inserts the specified element into the specified position
void Clear () Removes all elements
Boolean contain is the (Object o) List if the element is contained, which returns true
an int indexOf (Object o) returns the index of the first occurrence of the specified element in the list, and returns 1 without the element.
The Boolean isEmpty () list is empty and returns true
E Remove (int index) removes the specified position element
int size () returns the prime number of elements
Object[] ToArray () returns an array of all elements
<T> t[] ToArray (t[] a)
(PS: Set can print output directly)

2. Iterator iterator
The iterator method in 2.1 collection
Iterator<e> Iterator () returns an iterator that iterates over the elements of this collection.
2.2 Itetrator<e> Interface
The Boolean hasnext () still has elements that can iterate, returning true
E Next () returns the next element of the iteration
void Remove () removes the last element returned by the iterator from the collection pointed to by the iterator

1 /*2 * Use iterator to iterate through the collection, assuming that the collection coll is a string object3 */4Iterator it =coll.iterator ();5  while(It.hasnext ()) {6String s =(String) It.next ();7 System.out.println (s);8 }9 Ten //Note: The procedure in the iteration cannot be set by the element in the Remove collection, otherwise it throws One java.util.ConcurrentModificationException Concurrency Modification exception, only the object pointed to by the current iterator can be removed through the Remove method of the iterator iterator AIterator it =coll.iterator (); -  while(It.hasnext ()) { -String s =(String) It.next (); the     if(S.equals ("111")){ -Coll.remove (s);//This can't be changed to It.remove () -     } -}

Java Learning Diary-11 Set (1)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.