Object-Oriented Programming: Abstract Data Types in Java

Source: Internet
Author: User

 


In this article, we will examine the data types in Java, but we will introduce the concept of abstract data types (ADT. We will also introduce Java Collections Framework (Java Collection architecture) to learn some ADTs defined by Java.

ADT
An ADT is defined only by the stored data type and operations that may be performed on this data type. Developers can only access the attributes of ADT through the ADT operation method, and they do not know how to implement various internal operations of this data type.

In Java, we often use an interface to provide an operation set without disclosing the implementation details of these operations. Remember that an interface defines a method set and a Java class must implement this set to meet its mandatory conditions or implement an instance of this interface.

Linear tables, stacks, and queues
When talking about ADT, we often talk about linear tables, stacks, and queues. We will not discuss the details of these data structures, but we will discuss why they are called ADT.

A linear table is a set of finite elements. Its elements are arranged in a linear manner and directly accessible to its elements. A stack is an ordered linear table of the last-in-first-out (LIFO). elements are added from the stack header and retrieved from the stack header. A queue is a first-in-first-out ordered linear table. elements are added from the end of the queue and retrieved from the queue header.

The internal structure of linear tables, stacks, and queues can be implemented in many ways. For example, we can use an ordered array or a linked list to implement each structure. The key point is that no matter how you implement its internal structure, its external interfaces remain unchanged. This allows you to modify or upgrade the underlying implementation process without changing the public interface.

Java Collection Architecture
Java 2 software development kit (SDK) provides some new classes to support most commonly used ADTs. These classes are called Java Collection classes (similar to the Collection classes in MFC), which work together to form a Java Collection architecture. This collection architecture provides a set of interfaces and classes that represent the so-called abstract data of a set.

The java. util. Collection interface is used to represent any group of objects, that is, elements. This interface provides basic operations such as adding, deleting, and querying. The Collection interface also provides an iterator method. The iterator method returns an instance of the java. util. Iterator interface. The Iterator interface provides hasNext, next, and remove methods. Using the Iterator interface, you can traverse an instance in a Collection object from start to end and safely delete the elements represented by iterator.

Java. util. AbstractCollection is the basis of all set architecture classes. The AbstractCollection class provides implementation for all methods except the iterator and size methods in the java. util. Collection interface. The two exception methods are implemented by all subclasses that inherit java. util. AbstractCollection.

Classes that implement an interface must implement all interface methods. Some interface methods in the collection architecture are optional, so there must be a way to notify the caller that some method is not implemented. When an optional method is implemented and this method is not implemented, an UnsupportedOperationException will be thrown. The UnsupportedOperationException class inherits the RuntimeException class. This allows the caller to call all set operations without placing each call in a try-catch pair.

List linear table
The List interface inherits the Collection interface and defines an ordered set that allows the existence of the same element. The List interface also attaches some methods that use a numeric index value and operate the elements in the Collection based on the position of the elements in the linear table. These operations include add, get, set, and remove.

The listIterator method is also provided for the List interface. This method returns an instance of the java. util. ListIterator interface. This instance allows you to traverse a linear table from the beginning to the end or from the end to the header. Java. util. ListIterator inherits the java. util. Iterator interface. Therefore, it supports adding and modifying elements in the Collection that it represents.

The following example shows how to traverse a list element from the back to the front. To do this, you must locate ListIterator after the last element of the list before the traversal starts.


ListIterator iter = aList. listIterator (aList. size ());

While (iter. hasPrevious ())

System. out. println (iter. previous (). toString ());

}

The collection architecture providesThe List interface has two implementations: List (linked List) and ArrayList (array List, that is, static List ). Both implementations support random access to their elements. An ArrayList instance supports Array Operations and array size changes. An example of a consumer list provides explicit support for adding, deleting, and providing elements at the beginning and end of the list. With these new methods, a programmer can simply use a LinedList as a stack or queue, as follows:


Upload list aQueue = new upload list (aCollection );

AQueue. addFirst (newElement );

ObjectAnElement = aQueue. removeLast ();

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.