Henry Instructions - NET Data Structure Single List (3)

zhaozj2021-02-16  59

Henry Instructions - NET Data Structure Single List (3)

Han Rui (06/15/2003)

3.8 ITEM properties

The Item property is supplied to VB.NET (2) to operate the list in such a way, which will also give a single-strand list and an ArrayList array, although it is not as good as an array, but such a convenient operating means we can't let go :

Default Public Overridable property item (_

Byval Index as integer) AS Object Implements IList.Item

The 'Item property, the data value of the index of the read and written list

Get

Validate (INDEX)

Return FindbyIndex (Index) .DATA

END GET

SET (ByVal Value As Object)

Validate (INDEX, VALUE)

Findbyindex (index) .data = value

End set

End Property

Similarly, we also provide a counting function, which is used by the NodeCount members in the above various operations.

Public overridable readonly property count () AS integer_

Implements ilist.count

'Return to the total number of linked tables

Get

Return NodeCount

END GET

End Property

Here, is we have implemented basic listing operations? However, there are still some important mechanisms that have not revealed to readers, let us continue.

3.9 Implementation of for Each Cycle Enumeration

For each is a very strong traversal method. To implement this feature, the chain table must implement the IEnumerable interface, and we reach this in ILIST we have achieved this goal by implementing the GetEnumerator function from the Ienumerable. But the function returns the IEnumerator enumerator type, so we have to achieve its own enumerator to achieve the handling of the list:

IEnumerator is a base interface for all enumerations. The number of enumerations only allows data in the collection. The number of enumerations cannot be used to modify the base collection. The Ienumerator interface supports two methods and an attribute. The MOVENEXT method can move a record in a collection. The reset method enables the enumerator to reset the start of the collection. Current read-only performance returns the current record from a collection.

Initially, the number of enumerated numbers is positioned in front of the first element in the collection. RESET also returns the enumeration number to this location. At this location, calling Current will trigger an exception. Therefore, before reading the value of the CURRENT, MoveNext must be called to advance the enumeration number to the first element of the collection.

Current returns the same object before calling MoveNext or RESET. MoveNext Sets Current to the next element.

After passing to the end of the collection, the enumeration number is placed behind the last element in the collection, and the calling MoveNext will return false. If the last time call MoveNext returns false, calling Current will lead to an exception. To set the current to the first element of the collection again, you can call the RESET and then call MoveNext.

As long as the collection remains the same, the number is maintained. If the collection is changed (eg, add, modify, or delete an element), the number of the enumeration will be invalid and unrecoverable, and the next time the next time the call will raise InvalidOperationException. If the collection is modified between MoveNext and Current, even if the number of enumerated is invalid, the Current will return the elements it set. Now you will understand the usage of the Version we have continuously seen in front. Since we have defined ListNode as nested classes, we need to call members of the SLLIST class, so place the custom implementation IENUMERATOR interface into the SLLIST class, becoming the second nested class:

Public overridable function genumerator () AS IENUMERATOR _

Implements IList.GeteNumerator

Return New SllistenceUmerator (ME)

END FUNCTION

Below is a class that implements the Ienumerator interface.

Protected Class SllistenceUmerator

Implements IENUMERATOR

'Nested countercraft in SLLIST

Protected List as Sllist

Protected Currentelement As Object

Protected currentnode as listNode 'Nested internally can use this class

Protected version as integer 'internal version records for comparison if there is a change in the chain list

Public Sub New (Byval List as Sllist)

'initialization

Me.list = list

Me.Version = list.version

Me.currentelement = list

Me.currentnode = list.head

End Sub

Protected Overridable Sub VerifyListisunchanged ()

'The determination version has changed after the enumerator is created.

If not version = list.version then

Throw new INVALIDOPERATIONEXCEPTIONEXCEPTIONEXCEPTION

"This chain list changes after the enumerator is created")

END IF

End Sub

Public overridable readonly property current () AS Object_

Implements IENUMERATOR.CURRENT

'Return to the current record

Get

'Determine if it has reached the tail or whether to call MoveNext Find Record

IF currentelement is listim

IF currentnode is list.Head Then

Throw new INVALIDOPERATIONEXCEPTIONEXCEPTIONEXCEPTION

"The Current method is invalid before the MoveNext is called.")

Else

Throw new INVALIDOPERATIONEXCEPTIONEXCEPTIONEXCEPTION

"I have reached the collection tail, so the Current method is invalid")

END IF

END IF

Return Currentelement

END GET

End Property

Public overridable function moveXExt () as boolean _

Implements IENUMERATOR.MOVENEXT

'Continue your enumerator to the next object

VerifyListisunchanged ()

IF not currentnode.nextnode is nothingatenode = currentnode.nextNode

Currentelement = currentnode.data

Return True

Else

Currentelement = LIST

CurrentNode = list.head

Return False

END IF

END FUNCTION

Public Overridable Sub Reset () Implements IENUMERATOR.RESET

'Reset the enumerator to its initial position

VerifyListisunchanged ()

CurrentNode = list.head

Currentelement = LIST

End Sub

END CLASS

----

Disclaimer: The right to copyright and interpretation of this article belongs to Han Rui, if you need to reprint, please keep your full content and this statement.

QQ: 18349592

E-mail: Henry7685@hotmail.com

Please visit my column:

http://www.9cbs.net/develop/author/netauthor/ilatitude/

转载请注明原文地址:https://www.9cbs.com/read-25538.html

New Post(0)