Using Java to implement object-oriented sixth chapter

Source: Internet
Author: User

Sixth Chapter Set Frame

One: interface: The abstract data type that represents the collection.

Implementation: That is, the implementation of the interface in the collection framework.

Algorithm: A method that accomplishes a useful calculation on an object that implements an interface in a collection frame, such as finding,

Sorting, and so on.

Collection interface stores a set of non-unique, unordered objects

The List interface stores a set of objects that are not unique, ordered (in order of insertion)

The set interface stores a unique, unordered set of objects

The map interface stores a set of key-valued objects that provide a key-to-value mapping

Second, List interface

Common classes for the list interface are ArrayList and LinkedList. They can all accommodate all types of objects, including

Null, allows duplicates, and guarantees the order in which elements are stored.

ArrayList implements a variable-length array that allocates contiguous space in memory. High efficiency for traversing elements and random access elements

The LinkedList uses the list storage method. High efficiency when inserting and deleting elements.

1.List Interface Common Methods

Method name

Description

Boolean Add (Object o)

Add an element at the end of the list, starting at index 0

void Add (int index,object o)

Adds an element at the specified index location. The index location must be between 0 and the list of meta

Between the number of vegetarian

int size ()

Returns the number of elements in a list

Object get (int index)

Returns the element at the specified index position. The removed element is of type object and requires a forced type conversion before use

Boolean contains (Object o)

Determines whether the specified element exists in the list

Boolean remove (Object o)

Remove an element from the list

Object Remove (int index)

Removes the specified position element from the list, starting at the starting index position from 0

Special methods for 2.LinkedList

Method name

Description

void  AddFirst (Object o) &nb sp;  

add element at header of list

void  addlast (Object o)

add element at the end of the list

object      GetFirst ()

Returns the first element in a list

object      GetLast ()

Returns the last element in the list

object      RE MoveFirst ()

Delete and return the first element in the list

Object removelast () deletes and returns the last element in the list

Common methods of Map

Method Name Description

Object put (Object key, Object val) is stored in a "key-value pair" manner

Object get (Object key) returns the associated value according to the key, or null if the specified key does not exist

Object remove (object key) deletes the "key-value pair" that is mapped by the specified key

int size () returns the number of elements

Set KeySet () returns a collection of keys

Collection values () returns a collection of value

Boolean ContainsKey (Object key) returns True if there is a "key-value pair" that is mapped by the specified key

Similarities and differences of Hashtable and HashMap P190

Third, iterator iterator

All the collection interfaces and classes do not provide a corresponding traversal method, but rather the collection traversal is implemented by iterator

The iterate () method of the Collection interface returns a iterator, which is then implemented by the two methods of the iterator interface.

Traverse

1.boolean Hasnext (): Determine if there is another accessible element

2.Object Next (): Returns the next element to be accessed

Iv. Generic Collections

Puts any type of object into the list with Add (Object obj), which is considered to be an object type

Forced type conversion is required to remove elements from the list through get (int index), cumbersome and prone to exceptions

The same problem exists when using the map's put (object key, Object value) and get (object key) to access the object

Same problem when retrieving elements using Iterator's next () method

Using Java to implement object-oriented sixth chapter

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.