30.07.2013 Views

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 23 Data Structures and Collections 1185<br />

Count property. This reduces the s<strong>to</strong>rage capacity of the ArrayList <strong>to</strong> the exact number<br />

of elements currently in the ArrayList.<br />

When users click Statistics, cmdStatistics_Click (lines 125–130) uses the<br />

Count and Capacity properties <strong>to</strong> display the current number of elements in the<br />

ArrayList and the maximum number of elements that can be s<strong>to</strong>red without the allocation<br />

of more memory <strong>to</strong> the ArrayList.<br />

When users click Display, cmdDisplay_Click (lines 133–144) outputs the contents<br />

of the ArrayList. This event handler uses an IEnumera<strong>to</strong>r (sometimes called<br />

an enumera<strong>to</strong>r, or an itera<strong>to</strong>r) <strong>to</strong> traverse the elements of an ArrayList one element at<br />

a time. Interface IEnumera<strong>to</strong>r defines methods MoveNext and Reset and property<br />

Current. MoveNext moves the enumera<strong>to</strong>r <strong>to</strong> the next element in the ArrayList. The<br />

first call <strong>to</strong> MoveNext positions the enumera<strong>to</strong>r at the first element of the ArrayList.<br />

MoveNext returns True if there is at least one more element in the ArrayList; otherwise,<br />

the method returns False. Method Reset positions the enumera<strong>to</strong>r before the first<br />

element of the ArrayList. Methods MoveNext and Reset throw an InvalidOperationException<br />

if the contents of the collection are modified after the enumera<strong>to</strong>r’s<br />

creation. Property Current returns the object at the current location in the ArrayList.<br />

Line 136 creates an IEnumera<strong>to</strong>r, called enumera<strong>to</strong>r, and assigns it the result of<br />

a call <strong>to</strong> ArrayList method GetEnumera<strong>to</strong>r. Lines 139–141 use enumera<strong>to</strong>r <strong>to</strong><br />

iterate the ArrayList (as long as MoveNext returns True), retrieve the current item<br />

via property Count and append it <strong>to</strong> StringBuilder buffer. When the loop terminates,<br />

line 143 displays the contents of buffer.<br />

23.7.3 Class Stack<br />

The Stack class (namespace System.Collections) implements a stack data structure.<br />

This class provides much of the functionality that we defined in our implementation<br />

in Section 23.4. The application in Fig. 23.29 provides a GUI that enables the user <strong>to</strong> test<br />

many Stack methods. Line 31 of the FrmStackTest construc<strong>to</strong>r creates a Stack with<br />

the default initial capacity (10 elements).<br />

Class Stack provides methods Push and Pop <strong>to</strong> perform the basic stack operations.<br />

Method Push takes an Object as an argument and adds it <strong>to</strong> the <strong>to</strong>p of the Stack. If the<br />

number of items on the Stack (the Count property) is equal <strong>to</strong> the capacity at the time of<br />

the Push operation, the Stack grows <strong>to</strong> accommodate more Objects. Event handler<br />

cmdPush_Click (lines 37–42) uses method Push <strong>to</strong> add a user-specified string <strong>to</strong> the<br />

stack (line 40).<br />

1 ' Fig. 23.29: StackTest.vb<br />

2 ' Demonstrates class Stack functionality.<br />

3<br />

4 Imports System.Collections<br />

5 Imports System.Text<br />

6 Imports System.Windows.Forms<br />

7<br />

8 Public Class FrmStackTest<br />

9 Inherits Form<br />

Fig. 23.29 Stack class demonstration (part 1 of 4).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!