In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "what are the abstract data types in Java". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn "what are the abstract data types in Java"?
ADT
An ADT is defined only by the saved data type and the operations that may take place on that data type. Developers can only access the properties of ADT through ADT's operation methods, and they don't know how various operations within this data type are implemented.
In Java, we often use an interface to give a set of operations without revealing the details of the implementation of those operations. Remember that an interface defines a set of methods and the Java class must implement this collection to meet its mandatory conditions or to implement an instance of the interface.
Linear tables, stacks and queues
When we talk 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 collection of finite elements whose elements are arranged in a linear manner and provide direct access to its elements. A stack is an ordered last-in, first-out (LIFO) linear table, with elements added from and taken out of the stack header. A queue is an ordered first-in-first-out linear table with elements added from the end of the queue and taken out from the head of the queue.
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 interface remains the same. This allows you to modify or upgrade the underlying implementation without changing the common interface part.
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), and they work together to form a Java collection architecture. This collection architecture provides a set of interfaces and classes that represent data as so-called collection abstract data.
The java.util.Collection interface is used to represent arbitrary groups of objects, that is, elements. This interface provides basic operations such as add, delete, and query. 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 methods provided by the Iterator interface, you can loop through an instance of a Collection object and safely delete the elements represented by iterator (cursors).
Java.util.AbstractCollection is the foundation of all collection schema classes. The AbstractCollection class provides an implementation of all methods in the java.util.Collection interface except the iterator and size methods. These two exception methods are implemented by all subclasses that inherit java.util.AbstractCollection.
A class that implements an interface must provide an implementation of all interface methods. Because some of the interface methods in the collection architecture are optional, there must be a way to notify the caller that a method is not implemented. When an optional method is implemented and the method is not implemented, a UnsupportedOperationException exception is thrown. The UnsupportedOperationException class inherits the RuntimeException class. This allows the caller to invoke 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 ways to manipulate elements in Collection using a numeric index value and based on the location in the element's linearity table. These operations include add,get,set and remove.
The List interface also provides a listIterator method. This method returns an instance of the java.util.ListIterator interface that allows you to traverse a linear table from beginning to end or from end to end. Java.util.ListIterator inherits the java.util.Iterator interface. Therefore, it supports the addition and modification of the elements in the Collection it represents.
The following example shows how to traverse the elements of a list from back to front. To do this, ListIterator must be positioned after an element in the list * before the 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 (linked list) and ArrayList (array list, or static list). Both implementations support random access to their elements. An ArrayList instance supports array-style operations and array resizing operations. An example of LinkedList 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:
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 to demonstrate some common operations on an implementation example of the java.util.List interface. These operations include adding elements, randomly accessing elements, and explicitly deleting elements at the end of the list.
It's good to know but don't know why.
ADT provides a powerful tool to separate operations in an object's public interface from its concrete implementation. This allows an ADT implementation to constantly change and evolve while keeping its common interface unchanged. The Java collection architecture provides a large number of interfaces and their implementations to represent collections of basic elements and can be used to create useful ADT.
At this point, I believe you have a deeper understanding of "what is the abstract data type in Java". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.