The array has changed a lot in vb.net. First, as described in the Simple Assignment section: The index number of an array is always zero-based, and the element type of the array is strongly typed, that is, each element contained in the array must be of the same type.
Array Object
Using the Dim Iarr (5) as Int32 in vb.net is actually an instance of an array object that is declared. The array object is located in the System namespace. This object provides methods for creating, manipulating, searching, and sorting arrays, and thus serves as the base class for all arrays in the common language runtime. However, although the array class is the base class for language implementations that support arrays. However, only systems and compilers can derive explicitly from the Array class. The user should use the array constructs provided by the language. The array object provides some important properties and methods that allow us to manipulate arrays.
Examples of array operations
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 = Iarr.getupperbound (0) to 0 Step-1 ' iarr.getupperbound (0) =5
' The elements of the array are
Iarr.setvalue (i, i)
Next
Array.Sort (Iarr) ' array elements sorted to 0,1,2,3,4,5
For i as Int32 = 0 to Iarr.getupperbound (0)
' Output array element
Next
ArrayList objects
The capacity of the Ayyar is fixed, VB. NET provides a ReDim statement that actually leverages the ability of a ArrayList object. ArrayList is a complex version of array. ArrayList actually treats the array as a collection, and of course can automatically expand the capacity as needed. Since ArrayList is handled as a collection, it is certainly in system. Collections in the namespace.
However, the array has its advantages over ArrayList: Array can have multiple latitudes, but ArrayList can have only one latitude. Value types do not need to be boxed and unloaded in the elements of an array, so array is more efficient than ArrayList in terms of value types. (The data type is described in the later Type section.) )
To change the size of an array object using the ArrayList object example
Dim Iarr (5) as Int32
For i as Int32 = 0 to Iarr.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) ' iarrlist.count=12
iarrlist.capacity = ' iarrlist.count=12 '
Iarrlist.trimtosize () ' iarrlist.count=12
Iarr = Iarrlist.toarray (GetType (Int32))
For i as Int32 = 0 to Iarr.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 is required (an array can be declared as any base data type, struct, or object class) and that all elements of the array must be of that 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, and so on).
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.