java--Collection

Source: Internet
Author: User

I. Overview of the collection

1.   Common features in Collection

      Boolean Add (Object e): Adding elements to the collection

void Clear (): empties all elements in the collection

Boolean contains (Object O): Determines whether an element is contained in the collection

Boolean IsEmpty (): Determines whether the elements in the collection are empty

Boolean remove (Object o): Deletes an element based on the content of the element

int size (): Gets the length of the collection

Object[] ToArray (): Ability to convert collections into arrays and store elements in the collection

2. iterators

      There are a number of collections in Java that are stored differently when storing elements. We're going to take out the elements in these collections, which can be done in a common way.

Collection The general acquisition of the elements of the collection: before taking the element to determine whether there are any elements in the set, if there is, take this element out, continue to judge, if there is another out.            All the elements in the collection have been taken out all the time. This method of extraction is known as an iterative term.

This method of fetching elements in the collection is described in the The iterator interface. The common methods of iterator interface are as follows

Hasnext () Method: Determine if there are elements in the set that can iterate

Next () method: Used to return the next element of the iteration and move the pointer back one bit.

3. Concurrency modification Exceptions

     When iterating through the collection using iterators, the increment /Delete method in the collection is used, causing the concurrency modification exception to occur

     Concurrency Modification Exception Resolution:

A: You can use the collection method to increment or delete a loop without using an iterator to traverse the collection

B: Iterator traversal is still used, then you need to use iterator's subinterface listiterator to implement adding to the collection

java--Collection

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.