Henry Instructions - NET Data Structure Object Silent Single List (4)

zhaozj2021-02-16  53

Henry Instructions - NET Data Structure Object Silent Single List (4)

Han Rui (06/15/2003)

3.10 Other properties

This article does not fully implement all ILIST interface methods and properties, but it is still to be declared as follows. Since the single-chain table class in this article does not provide a built-in thread synchronization (all finished, what is the next time I say. Haha), Issynchronized returns to false, Syncroot returns to the reference. The number of enumerations do not have an exclusive access to the collection; therefore, enumerating a collection in essence is not a thread security process. Even when synchronizing the collection, other threads can still modify the collection, which will cause an enumeration number to trigger an exception. To ensure thread security during enumeration, you can lock a collection through the SYNCLOCK throughout the enumeration process, or capture exceptions caused due to changes in other threads. The processing like the SSLIST class, each lock will block all objects, to provide a smaller thread safety, we will continue to discuss later.

Public overridable readonly property issynchronized () as boolean _

Implements ilist.issynchronized

Get

Return False

END GET

End Property

Public overridable readonly property syncroot () as object _

Implements ilist.syncroot

Get

Return ME

END GET

End Property

The isFixedSize property refers to a value when analog is implemented, which indicates whether the ilist has a fixed size. The chain table category does not have a fixed size, so the return is of course FALSE.

Public overridable readonly property isfixedsize () as boolean _

Implements IList.isfixedsize

Get

Return False

END GET

End Property

The isReadOnly property refers to a value indicating whether iList is read-only when an implementation is implemented. Of course, returning false.

Public overridable readonly property isreadonly () as boolean _

Implements IList.isreadOnly

Get

Return False

END GET

End Property

4. Call the example

Above we have analyzed in detail with the implementation of the SSLIST class, now come and see its structure, as shown in Figure 2:

Figure 2 structure (including two nested classes)

So how do you use our single-link table? In fact, it is easy, it is quite similar to the usage method of the ArrayList class, but we don't have to set the length of the list. In another VB file, we will operate the SLLIST class:

'Example of use

DIM LST ​​AS New SLLIST ()

Try

Lst.Add ("Henry") Add

Lst.Add ("jjj")

Lst.Insert (1, "kkk") 'insert

Lst.Remove ("jjj") 'Press value to delete

Lst.Removeat (1) 'Delete according to the index number

LST (0) = "jerry" "change

DIM I as integer = lst.indexof ("jjj") 'Removes the induced number' multiple loop traverses

DIM CollectionItem As Object

Dim loopcounter as integer

DIM ENUMCOLLECTION As IEnumerator

'The first

For Each CollectionItem in LST

Console.writeline (CollectionItem)

NEXT

'Second

For loopCounter = 0 to Lst.count - 1

Console.WriteLine (LST.Item (LoopCounter))

NEXT

'Third

Enumcollection = Lst.GeteNumerator ()

Do While Enumcollection.Movenext

Console.writeline (Enumcollection.current)

Loop

Catch exception

MsgBox (ex.toswoting)

END TRY

Now let's take a look at the role of Version:

'Third

Enumcollection = Lst.GeteNumerator ()

Lst.Add ("Kelly")

Do While Enumcollection.Movenext

Console.writeline (Enumcollection.current)

Loop

At this point, you will get an exception, prompt to:

System.invalidOperationException: This chain list has changed after the enumerator was created.

Isn't this an error message we defined in the VerifyListisunchanged method? Now everyone will understand why we have the corresponding prompt text when you operate ArrayList or other .NET classes. Imaginate the error prompts that we define our definition prompts will also be captured in this example. Please test it yourself.

5. Summary and Prospect

In this article, you will go deep into the implementation of the Collections collection class, and we use ArrayList (I am also, huh) in the actual work. However, the insertion of the linked list is much higher than that of ArrayList (single-linked list plus the front flute pointer will increase efficiency), of course, the ability of the index value is not as good as ArrayList. When we deal with certain lists of frequently deleting and insert, we are still worth considering whether to use a linked list to implement our needs.

Limited to space and time, for sorting, synchronization, etc. ARRAYLIST has no excessive involvement, interested friends, please continue to pay attention to my column. see you later!

----

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/latitude/

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

New Post(0)