VB.NET study notes (arrand)

zhaozj2021-02-16  47

Array

There is a big change in VB.NET in the VB.NET. The first thing is as described in the simple assignment chapter: The index number of the array is always from zero; and the elements type of the array is a strong type, that is, each element included in the array must be the same type.

Array object

Using DIM IARR (5) AS INT32 in VB.NET actually declares an instance of an Array object. The Array object is located in the system namespace. This object provides a method for creating, operating, searching, and sort arrays, so it is used as a base class for all arrays in a public language runtime. However, although the Array class is a base class that supports the language of array. However, only the system and compiler can explicitly derive from the Array class. Users should use the array structure provided by the language. The Array object provides some important properties and methods to facilitate our an array.

ARRAY array operation example

DIM IARR (5) AS INT32 'IARR.LENGTH = 6

DIM Barr (5, 5) as boolean 'barr.rank = 2, barr.length = 36

Array.clear (IARR, 0, IARR.LENGTH - 1)

For i as int32 = narr.getupperbound (0) to 0 step -1 'arr.getupperbound (0) = 5

The elements of the 'array are

IARR.SetValue (i, i)

NEXT

Array.Sort (IARR) 'array element is sorted by 0, 1, 2, 3, 4, 5

For i as int32 = 0 to arr.getupperbound (0)

'Output array elements

NEXT

ArrayList object

Ayyar's capacity is fixed, and the Redim statement provided by VB.NET is actually the ability to take advantage of the arraylist object. ArrayList is a complex version of Array. ArrayList actually treats Array as a collection, of course, can automatically expand capacity as needed. ArrayList is handled in a collection, so it is of course in system. Collections namespace.

However, Array also has its advantage over ArrayList: Array can have multiple latitudes, but ArrayList can only have a latitude. The type of element value in Array does not require operation of the packing and unloading, so Array has high efficiency than ArrayList in terms of value type. (The data type will be described in the later type chapter.)

Change Array Object Capacity Size

DIM IARR (5) AS INT32

For i as int32 = 0 to arr.getupperbound (0)

IARR.SetValue (i, i) '0, 1, 2, 3, 4, 5

NEXT

DIM IARRLIST AS New System.Collections.ArrayList (IARR) 'IARRLIST.COUNT = 6

For i as int32 = 6 to 10

IARRLIST.ADD (i)

Next 'IARRLIST.COUNT = 11

IARRLIST.INSERT (5, 99) 'IARRLIST.COUNT = 12

IARRLIST.CAPACITY = 100 'IARRLIST.COUNT = 12

Iarrlist.trimtosize () 'IARRLIST.COUNT = 12

IARR = IARRLIST.TOARRAY (Gettype (int32))

For i as int32 = 0 to arr.getupperbound (0) 'IARR.GetupperBound (0) = 11' Output: 0, 1, 2, 3, 4, 99, 5, 6, 7, 8, 9, 10

NEXT

An array declaration specifies that a data type (you can declare an array declaration as any basic data type, structure or object class), and all elements of the array must be this type of data. However, if the data type is Object, a single array element can contain various types of data (such as objects, strings, numbers, etc.).

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

New Post(0)