Implementation of stacks in Java class libraries

Source: Internet
Author: User

A stack is a last-in-first-out data structure. On top of it, there are three main operations:

(1) Determine if the stack is empty--empty ();

(2) Add an element--push (E) at the top of the stack;

(3) Delete and return the top element of the stack--pop ().

In the Java class Library, the stack class implements stacks, which inherit from the vector class:

public class Stack<e> extends vector<e>
The stack then saves the elements in an array:

protected object[] Elementdata;
Use an integer to record the number of elements in the stack:

protected int elementcount;
Let's look at how the stack's three operations are implemented in the stack.

1.
The empty () method is implemented as follows:

    public Boolean empty () {        return size () = = 0;    }

The size () method is the method defined in the vector, which is defined as follows:

public synchronized int size () {        return elementcount;    }

It returns the number of elements in the stack, meaning that if the number of elements in the stack is 0, the stack is empty.

2. Thepush (E Element) method is implemented as follows:

    Public e push (e item) {        addelement (item);        return item;    }

It calls the AddElement (E) method to implement adding elements to the stack,addelement (E)The method is implemented as follows:

    Public synchronized void addelement (E obj) {        modcount++;        Ensurecapacityhelper (Elementcount + 1);        elementdata[elementcount++] = obj;    }

You just need to focus on the last line of the method:

elementdata[elementcount++] = obj;

It adds an element to the previous array of elements and adds 1 to the element, which is required for the push operation of the stack.

3. Thepop () method is implemented as follows:

    Public synchronized e pop () {        e       obj;        int     len = size ();        obj = Peek ();        Removeelementat (len-1);        return obj;    }

It is already known that size () returns the number of elements in the stack, so Len equals Elementcount;peek () is another method in the stack that returns the top element of the stack, and removeelementat (int) is the method defined in the vector. Deletes the specified position element.

Peek () is implemented as follows:

    Public synchronized E Peek () {        int     len = size ();        if (len = = 0)            throw new Emptystackexception ();        Return ElementAt (len-1);    }

Peek ()Method ElementAt (int) returns the top element of the stack by invoking the method defined in the vector.the implementation of ElementAt (int) is as follows:

    Public synchronized E elementat (int index) {        if (index >= elementcount) {            throw new Arrayindexoutofboundsexcep tion (index + ">=" + Elementcount);        }        Return Elementdata (index);    }

elementat (int)Method calls the Elementdata (int) method again,elementdata (int)The method is implemented as follows:

    e elementdata (int index) {        return (E) elementdata[index];    }

Originally, the Peek () method actually returns the top element of the stack via elementdata[len-1].

Next look at the removeelementat (int) method:

Public synchronized void Removeelementat (int index) {        modcount++;        if (index >= elementcount) {            throw new ArrayIndexOutOfBoundsException (index + ">=" +                                                     elementcount);        }        else if (Index < 0) {            throw new arrayindexoutofboundsexception (index);        }        int j = elementcount-index-1;        if (J > 0) {            system.arraycopy (elementdata, index + 1, elementdata, index, j);        }        elementcount--;        Elementdata[elementcount] = null; /* To-let GC do it work */    }

The pop () is passed toremoveelementat (int)The parameters are len-1, that is, elementCount-1, that is to say,removeelementat (int)In the j=0, soremoveelementat (int)Method executes directly to:

        elementcount--;        Elementdata[elementcount] = null; /* To-let GC do it work */
These two lines by first reducing the number of elements in the stack by 1, and then set the previous stack top element to null so that it is garbage collector to remove the top element of the stack.




Implementation of stacks in Java class libraries

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.