Algorithm 1.1 down-pressure stack (LIFO) can dynamically adjust the size of the array __ algorithm

Source: Internet
Author: User
Tags int size iterable
algorithm 1.1 down-pressure stack (LIFO) can dynamically adjust the size of the array

The code implementation is as follows:

public class Resizingarraystack<item> implements iterable<item>{private item[] A = (item[)) New Object[1]  
    ;//stack element private int N = 0;//Element Quantity public boolean IsEmpty () {return N = 0;
    public int size () {return N;
        private void Resize (int max) {item[] temp = (item[]) new Object[max];
        for (int i = 0;i <n;i++) temp[i] = A[i];
    A = temp;
        public void push (item) {if (N = = a.length) resize (2*n);
    a[n++] = Item;
        Public Item Pop () {item = A[--n]; 
        A[n] = null;//Avoid free objects if (n > 0 && n = = A.LENGTH/4) resize (A.LENGTH/2);
    return item;
    Public iterator<item> iterator () {return new reservearrayiterator ();
        Private class Reservearrayiterator implements iterator<item>{private int i = N;
        public Boolean Hasnext () {return i > 0;Public Item Next () {return a[--i]; public void Remove () {}}}

Advantage: You can avoid wasting space and create an array without knowing how many arrays you need

Note that this stack implements the Iterable and iteration interfaces, which makes it easy to use the foreach syntax. If you want a collection data type to iterate, you must implement a iterator () method and return a iterator object, and you must also include two methods: Hasnext () (returns a Boolean value) and Next () (returns a generic element)

Iterable corresponds to the following interface:

Public interface iterable<item>
{
    iterator<item> iterator ();
}

You need to add a method iterator () to the class, as follows:

Public iterator<item> iterator () {return
    new Reversearrayiterator ();
}

The iterator interface is as follows:

Public interface iterator<item>
{
    boolean hasnext ();
    Item next ();
    void Remove ();
}

There is a private inner class inside the stack data type that implements the iterator interface and can access the instance variables of the class

Note: The Iterable interface is in Java.lang, but iterator is in java.util.

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.