Object-Oriented Programming: Abstract data types in Java _java programming

Source: Internet
Author: User
Tags data structures
Article Source: Internet Author: palesting/csdn

In this article, we'll look at the data types in Java, but we'll introduce the concept of abstract data types (ADT). We will also learn some ADT of Java definitions by introducing the Java Collections Framework (Java Collection Architecture).

   Adt

A ADT is defined only by the data types that are saved and the operations that may be performed on this data type. Developers can only access ADT properties through ADT Operations, and they do not know how the various operations within the data type are implemented.
  
In Java, we often use an interface to give a set of operations without disclosing the details of the implementation of these operations. Remember that an interface defines a set of methods that a Java class must implement to satisfy its mandatory conditions or to implement an instance of this interface.
  
   Linear tables, stacks, and queues

when we talk about ADT, we often speak of 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 linear table of LIFO (LIFO) in which elements are added from the stack header and removed from the stack header. A queue is a first-in, first-out, ordered linear table in which elements are added from the end of the queue and removed from the queue header.
  
Linear tables, stacks, and the internal structure of 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 is always the same. This allows you to modify or upgrade the underlying implementation without having to change the public interface section.

  Java Collection Schema

The Java 2 Software Development Kit (SDK) provides new classes to support most commonly used ADT. These classes are called Java collection classes (similar to the collection classes in MFC), and they work together to form a Java collection schema. 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 adding, deleting, and querying. The collection interface also provides a iterator method. The iterator method returns an instance of the Java.util.Iterator interface. The iterator interface also provides the Hasnext, next, and remove methods. Using the method provided by the iterator interface, you can iterate through an instance of a collection object and safely delete the elements represented by the iterator (cursor).
  
Java.util.AbstractCollection is the basis for all collection schema classes. The Abstractcollection class provides implementations of all methods except the iterator and size methods in the Java.util.Collection interface. The two exception methods are implemented by all inherited java.util.AbstractCollection subclasses.
  
A class that implements an interface must provide an implementation of all interface methods. Because some of the interface methods in the collection schema are optional, there must be a way to notify the caller that some 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 the collection operations without having to place each invocation in a Try-catch pair.
  
   List Linear table

The list interface inherits the collection interface and defines an ordered set that allows the same elements to exist. The list interface also attaches some methods that use a numeric index value and manipulate elements in the collection based on the position of the element in the linear 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, which allows you to traverse a linear table from beginning to end or from tail to head. Java.util.ListIterator inherits the Java.util.Iterator interface. Therefore, it supports additions and modifications to the elements in the collection that it represents.

The following example shows how to traverse the elements of a list from the back forward. To do this, you must position the listiterator after the last element of the list before the traversal begins.
  
Listiterator iter = Alist.listiterator (Alist.size ());
while (Iter.hasprevious ())
System.out.println (Iter.previous (). toString ());
}
The collection schema provides two implementations of the list interface: LinkedList (linked list) and ArrayList (array lists, or static lists). Both implementations support random access to their elements. An ArrayList instance supports array-style operations and supports an array size change operation. An LinkedList instance provides explicit support for adding, deleting, and providing elements at the beginning and end of a 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 fragment in table a demonstrates some common operations on the implementation instance of the Java.util.List interface using Java.util.ArrayList and java.util.LinkedList. These actions include adding elements, randomly accessing elements, and explicitly deleting an element at the end of the list.
  
   It's good to know it doesn't know why.

ADT provides a powerful tool for separating the operations of an object's public interface from its specific implementation. This allows the implementation of a ADT to change and evolve while maintaining its common interface unchanged. The Java Collection schema provides a large number of interfaces and a set of its implementations to represent the basic elements that can be used to create useful ADT.

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.