Object oriented programming: abstract data types in Java

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

An ADT is defined only by the saved data type and the operations that may be performed on that data type. Developers can only access the properties of ADT through the operation methods of ADT, and they will not know how various operations inside this data type are implemented. In Java, we often use an interface to give a collection of operations without revealing the details of the implementation of these operations. Remember that an interface defines a set of methods, and a Java class must implement this set in order to meet its mandatory conditions or implement an instance of the interface. Linear table, stack and queue when we talk about ADT, we often talk about linear table, stack and queue. We won't 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 whose elements are arranged in a linear manner and provide direct access to its elements. A stack is a last in first out (LIFO) is an ordered linear list. Elements are added from the stack header and taken out from the stack header. A queue is a first in first out ordered linear list. Elements are added from the end of the queue and taken out from the queue header. The internal structure of the linear list, stack and queue can be implemented in many ways. For example, we can use an ordered array or a linked list to implement each queue Structure. The key point is that no matter how you implement its internal structure, its external interface is always the same. This allows you to modify or upgrade the underlying implementation without changing the public interface.

Java collection architecture

The Java 2 software development kit (SDK) provides some new classes to support most commonly used ADT. These classes are called Java collection classes (similar to collection classes in MFC), they work together to form a Java collection architecture. This collection architecture provides a set of interfaces and classes to represent data as so-called collection abstract data. The java.util.collection interface is used to represent any group of objects, that is, elements. This interface provides basic functions such as adding, deleting, and querying Operation of. The collection interface also provides an iterator method. The iterator method returns Java util. An instance of the iterator interface. The iterator interface provides hasnext, next, and remove methods. Using the methods provided by the iterator interface, you can cycle through the instances in a collection object from beginning to end and safely delete the elements represented by the iterator (cursor).       java. util. Abstractcollection is the foundation of all collection schema classes. The abstractcollection class provides access to Java util. The implementation of all methods in the collection interface except the iterator and size methods. These two exceptional methods are inherited by all Java util. Subclass implementation of abstractcollection. Classes that implement an interface must provide implementations of all interface methods. Because some interface methods in the collection architecture are optional, there must be a way to inform the caller that a method is not implemented. When an optional method is implemented and this method is not implemented, an unsupported operationexception is thrown. The unsupported operationexception class inherits the runtimeException class. This allows the caller to call all collection operations without having to put each call in a try catch pair. List linear table the list interface inherits the collection interface and defines an ordered collection that allows the same elements to exist. The list interface also adds 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 list interface also provides the listiterator method. This method returns Java util. An instance of the listiterator interface, which allows you to traverse a linear table from head to tail or from tail to head. java. util. Listiterator inherits Java util. Iterator interface. Therefore, it supports the addition and modification of elements in the collection it represents.

The following example demonstrates how to traverse the elements of a list from back to front. To do this, you must position the listiterator after the last element of the list before traversal begins.       ListIterator iter = aList. listIterator(aList.size());    while (iter.hasPrevIoUs())    System. out. println(iter.prevIoUs(). toString()); } the collection architecture provides two implementations of the list interface: LinkedList and ArrayList (array list, i.e. static list). Both implementations support random access to its elements. An ArrayList instance supports array style operations and array size change operations. An LinkedList instance provides explicit support for adding, deleting and providing elements at the beginning and end of the list. Using these new methods, a programmer can simply A linedlist is used as a stack or queue, as follows: LinkedList aqueue = new LinkedList (acollection);    aQueue. addFirst(newElement);    Object anElement = aQueue. removeLast();    LinkedList aStack = new LinkedList(aCollection);    aStack. addFirst(newElement);    Object anElement= aStack. removeFirst(); The code snippet in table a uses Java util. ArrayList and Java util. LinkedList demonstrates the use of Java util. List interface implementation examples of some common operations. These operations include adding elements, randomly accessing elements, and explicitly deleting elements at the end of the list. Knowing what it is and not knowing why is of great benefit. ADT provides a powerful tool to separate the operation in the object public interface from its specific implementation. This allows the implementation of an ADT to change and evolve while keeping its common interface unchanged. The Java collection architecture provides a large number of interfaces and their implementations to represent the collection of basic elements and can be used to create useful ADTs.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>