Queu Queue: A lost Java.util class (End)

zhaozj2021-02-12  189

Queue Queue: A lost Java.util class

The second method

In order to overcome the performance issues mentioned above, the realization of the Queueue class in this method is based on the link table LinkedList class because the link table LinkedList for the first element's removal will not cause the movement of the other remaining elements. Public Class Queue2 Extends LinkedList {Public Object ENQUEUE (Object ELEMENT) {Add (ELEMENT); Return Element;}

Public Object Dequeue () {if (size () == 0) throw new EmptyqueueException (); return removefirst ();}}

However, it is the inheritance relationship applied in the method leads to a problem. Make the Queue2 Class user can call some function methods like addfirst, getlast, and more, resulting in some errors that cannot be expected.

Third method

In this method, the object combination will replace the inheritance mode. In general, the object portability is better than inheritance, which provides a smaller and more concentrated class and smaller inheritance level. Many designers use inheritance, resulting in a huge inheritance relationship, so that it becomes It is difficult to maintain and handle. A object-based design is a relatively small class file, but it will produce more objects.

PUBLIC CLASS Queue {

Private LinkedList item;

Public Object Enqueue (Object Element) {items.add (element); Return Element;

Public Object Dequeue () {if (items.size () == 0) throw new emptyqueueexception (); return items.removefirst ();}}

This implementation implements a more powerful, more concentrated queue quene class, but developers need to explicitly encode processing for all required interfaces (like size, empty, indexof, etc.). This complete code can be downloaded:

Download code

Translated by windowsdna 2004/01/08

转载请注明原文地址:https://www.9cbs.com/read-6199.html

New Post(0)