[Careercup] 14.6 Circulararray ring Array

Source: Internet
Author: User

14.6 Implement a Circulararray class that supports an array-like data structure which can be efficiently rotated. The class should use a generic type, and should support iteration via the standard for (OBJ O:circulararray) notation.

This problem allows us to implement a ring array class Circulararray, because the ring array needs to call rotate (int shiftright) function, here, we do not really go to rotate the array, because this is very not efficient. We use another implementation method, using a variable head to record the starting position of the ring array, then calling rotate is actually changing the head position. Please see the following code:

 Public Static classCirculararray<t>ImplementsIterable<t> {    Privatet[] items; Private intHead = 0;  PublicCirculararray (intsize) {Items= (t[])NewObject[size]; }        Private intConvertintidx) {if(IDX < 0) {idx+=items.length; }        return(head + idx)%items.length; }         Public voidRotateintshiftright) {Head=convert (shiftright); }         PublicT Get (inti) {if(I < 0 | | I >=items.length) {Throw NewJava.lang.IndexOutOfBoundsException ("..."); }        returnItems[convert (i)]; }         Public voidSetintI, T item) {Items[convert (i)]=item; }         PublicIterator<t>iterator () {return NewCirculararrayiterator<t> ( This); }        Private classCirculararrayiterator<ti>ImplementsIterator<ti> {        Private int_current =-1; Privateti[] _items;  PublicCirculararrayiterator (circulararray<ti>Array) {_items=Array.items; } @Override Public BooleanHasnext () {return_current < Items.length-1; } @Override PublicTI Next () {++_current; TI Item=(TI) _items[convert (_current)]; returnitem; } @Override Public voidRemove () {Throw NewUnsupportedoperationexception ("..."); }    }}

The code above has two main parts:

1. Implementing the Circulararray Class

The implementation of the process is prone to make some mistakes, such as:

-We cannot create a new array of generic classes, we must cast an array or define a type of list<t>

-Take the remainder operator% for negative operations will get negative numbers, which and mathematicians defined by the remainder of the operation is different, so we need to add items.length to the negative sequence, the period becomes positive and then do the operation.

-we must consistently ensure that the original sequence is converted to a rotational sequence.

2. Implementing an iterator Iterator interface

In order to iterate through the array for (OBJ O:circulararray), we must implement the iterator iterator interface:

-Modify the definition of circulararray<t> and add implements Iteratble<t>. This requires that we add a iterator () method to circulararray<t>.

-Establishment of Circulararrayiterator<t> class implements ITERATOR<T> This requires some of the methods we implement Circulararrayiterator, such as Hasnext (), Next (), and remove ().

Once we have implemented the above two items, the for (OBJ O:circulararray) loop will magically run.

[Careercup] 14.6 Circulararray ring Array

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.