Algorithm (4th edition) -1.3.2 implementation of collection class data types

Source: Internet
Author: User

Summary: This section first gives a simple and classic implementation, and then discusses its improvements from generics, resizing data, object dissociation, and iteration.

Focus:

1. Constant capacity stack:

· Only string values can be processed

· Require a use case to specify a capacity

· Iterations are not supported

2. We want to create an array of generics in the implementation of the Fixcapacitystack constructor with the following code:

New ITEM[CAP];

For some historical and technical reasons (not covered by this book), creating generic arrays is not allowed in Java. We need to use type conversions:

New OBJECT[CAP];

This code will do what we expect (but the Java compiler will give a warning, but we can ignore it), and we'll always use this approach in this book (the same approach is used in implementations of similar abstract data types in the Java System Library).

3. Choosing An array representation stack means that the use case must anticipate the maximum capacity of the stack.

4. We can modify the implementation of the array, dynamically adjust the size of the array a[], so that it is sufficient to save all the elements, without wasting too much space:

If there is no extra space, we will double the length of the array;

If the stack size is less than One-fourth of the array, we will halve the length of the array.

In this implementation, the stack never overflows, and the usage is never less than One-fourth (unless the stack is empty, in which case the array size is 1).

The textbook has the source code, fully understand!

5. Even if the use case no longer requires this element, the reference in the array will still allow it to exist. This situation, which holds a reference to an unwanted object, is called a free.

In this case, it is easy to avoid object dissociation by simply setting the value of the array element that is ejected to null, which overwrites the useless reference and allows the system to reclaim its memory after the use cases have been used to eject the element.

6. Because (for some historical reasons) iterator is not in Java.lang (although Iterable is part of Java.lang).

Algorithm (4th edition) -1.3.2 implementation of collection class data types

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.