Java Programming thought notes (holding objects)

Source: Internet
Author: User

1. Generic and type-safe containers

2. Basic Concepts

3. Add a set of elements: The Arrays.aslist () method takes an array or a comma-delimited list of elements (using mutable arguments) and converts it to a list object. The Collections.addall () method takes an collection object, as well as an array or a comma-separated list, to add elements to the collection. The Collection.addall () member method can only accept another Collection object as a parameter, so it is not as flexible as arrays.aslist () or Collections.addall (), and both methods use a mutable argument list.

4. Printing of containers: List, which stores a set of elements in a particular order; Set, elements cannot be duplicated; Queue, only one "end" of the container is allowed to insert the object, and the object is removed from the other "side". Map holds two objects in each slot, the key and the value associated with it.

5.List: ArrayList, which is longer than random-access elements, but is slower to insert and remove elements in the middle of a list. LinkedList, which provides optimized sequential access through a lower-cost insert and delete operation in the middle of the list.

6. Iterators: Java's iterator can only be moved in one direction, and this iterator can only be used to: 1. Using method iterator () requires the container to return a iterator. Iterator will be ready to return the first element of a sequence. 2. Use next () to get the next element in the sequence. 3. Use Hasnext () to check if there are elements in the sequence. 4. Use remove () to delete the newly returned element of the iterator.

6 (1). Listiterator: can only be used for access to various list classes; it is possible to move in two directions, producing the index of the previous and subsequent elements relative to the current position pointed to by the iterator in the list, and using the set () method to replace the last element it accesses , by calling the Listiterator () method to produce a pointer to the listiterator at the beginning of the list, and by calling the Listiterator (n) method to create a listiterator at the beginning of the element that points to the list index n.

7.LinkedList

8.Stack: " stack" is usually referred to as a "last in, first out" container. Push () accepts objects of type T, and Peek () and Pop () return objects of type T. The Peek () method provides a top-of-stack element, but does not remove it from the top of the stack.

9.Set

10.Map

11.Queue: The queue is a typical FIFO container. The offer () method is one of the methods associated with the queue, which, if allowed, inserts an element into the tail of the team, or returns false. Both peek () and element () will return to the head without removing, but the Peek () method returns NULL when the queue is empty, and element () throws a Nosuchelementexception exception. The poll () and remove () methods Remove and return a team header, but poll () returns NULL when the queue is empty, and remove () throws a Nosuchelementexception exception.

11 (1). Priorityqueue: The priority queue declares that the next pop-up element is the most needed element (with the highest priority).

12.Collection and iterator

13.Foreach and Iterators

13 (1). Adapter method Customary method

Add:iterator is an iterator class, and iterable is an interface. Because the core method of the Iterator interface next () or Hasnext () is dependent on the current iteration position of the iterator. If collection implements the iterator interface directly, it is bound to cause the collection object to contain the data (pointers) of the current iteration position. When a collection is passed between different methods, the result of the next () method becomes unpredictable because the current iteration position is not pre-provisioned. unless you add a reset () method to the iterator interface, you can reset the current iteration position. But in this case, collection can only have one current iteration position at a time. Instead, Iterable returns an iterator that counts from the beginning of each call. multiple iterators are non-interfering.

Java Programming thought notes (holding objects)

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.