vb.net array definitions Dynamic use of multidimensional arrays

Source: Internet
Author: User

We all know that arrays are a very important concept in all programming languages, and the function of an array is to agree that the program ape uses the same name to refer to multiple variables, and therefore uses an array index to differentiate the variables. In very many cases, the array index is used to set up a loop so that complex situations can be handled efficiently, so using arrays in very many cases can shorten or simplify the code of the program. This article mainly introduces the use of vb.net array, hoping to bring help to everyone's use.


The subscript of the first element in the array is called the Nether, the last element's subscript is called the upper bound, the remaining elements are continuously distributed between the upper and lower bounds, and the array is stored in memory in contiguous regions, so it is required to declare that the length of each dimension of the array cannot exceed the maximum value of the long data type, i.e. 264-1=263.

We put VB. NET array is treated as an object, which means that the array type is a single reference type, and the arrays variable includes pointers to data such as array elements, arrays, and array lengths, and the arrays are in fact simply copying pointers, and arrays inherit the array class of the System namespace.

VB. There are some differences between the array types provided in NET and VB 6.0, we will do a specific explanation below.

(1) VB. NET array of declarations

VB. There are two types of arrays in net: fixed-length arrays and dynamic arrays. This first introduces several different ways of declaring a fixed-length array, and different declarative methods will result in different valid ranges for the array.

The Dim statement establishes a Module series group in the module segment, such as:

 
   
  
  1. Dim Arrayl (3) as Integer

The public statement establishes a common array in the Declaration section of the module, such as:

 
   
  
  1. Public CountType () as String

The static statement declares a local array within a procedure, such as:

 
   
  
  1. Public Sub Ipaddress ()
  2. Static Server (a) As String
  3. End Sub

VB. NET also provides a new array initialization syntax, which simply requires a simple statement to complete the declaration and initialization of the array, such as:

 
   
  
  1. New Array Initialization syntax
  2. Dim Arrayl as Integer () ={2,4,8}

In VB. NET, in order to be easier to work with other languages, the array subscript is set to 0, do not agree to declare an array of the Nether 1, so the option Base statement is no longer VB. NET support, and when declaring an array, it must be initialized with its number of elements, not its upper bounds, such as:

 
   
  
  1. Declares a one-dimensional array with 3 elements, subscript from 0~2
  2. Dim Arrayl (3) as Integer
  3. Arrayl (0): 2
  4. Arrayl (1) =4
  5. Arrayl (2) =8

The array declared above has three elements, subscript from 0 to 2, assuming the code attempts to access an array element labeled 3, will cause an execution error.

(2) Two-dimensional arrays and multidimensional arrays

In addition to a simpler one-dimensional array, VB. NET also supports multidimensional arrays, and its declaration methods and one-dimensional arrays are not much different, such as:

 
   
  
  1. StatiC Multidim (10,10) as Double
  2. Public Singledim (5,8,10,3) as single

The above statement declares an Lo row, a two-dimensional array of 10 columns. In VB. NET, the array has a maximum of 32 dimensions, and the length of each dimension can not exceed the maximum value of the long array type. The limit for the total size of the array is different, which is related to the operating system used and the amount of memory that the computer uses. The dimensions of the array remain in the system. Array. In the rank attribute, the length of each dimension can be determined by the system. Array. Getlengfll method to get. Caution is required because of VB. NET is based on 0, assuming that the returned value is 9, the array is 10-dimensional. When an array continues to add dimensions to a multi-bit array, the storage space required for the arrays is greatly added, so consider this aspect when working with multidimensional arrays.

In addition VB. NET also provides LBound () and LBound () two functions to return the upper and lower bounds of the array. China self-Study programming network finishing, www.zxbc.cn for a one-dimensional array, only one parameter is required, and that is the array name. Like what:

 
   
  
  1. One ==ubound (Arrayl)

For multi-bit arrays, it is simply the second parameter following the comma that specifies the dimension of the array. Like what:

 
   
  
  1. tw0 = Lbound (multidim,7)

Assuming that it does not indicate which dimension it is, the system defaults to the first dimension.

(3) Dynamic array

Sometimes the size of the array cannot be confirmed before the program executes, VB. NET provides the ability to dynamically determine the size of an array when the program executes, that is, a dynamic array. It has the flexibility to change the size of the array at any time, depending on the need, to help with memory management. The specific process for creating a dynamic array is as follows:

①, like declaring a general array, can use several of the declarations described earlier, simply assigning an empty-dimensional array, which declares the array as a dynamic array. A typical declaration statement is:

 
   
  
  1. Dim types () As Integer

② then uses the ReDim statement to configure the array size. ReDim Statement Declaration can only be used in the process, it is a statement can be run, can change the number of elements in the array, but can not change the dimensions of the array, that is, one dimension cannot be changed to two-dimensional. When the ReDim statement configures the number of array elements, the contents of the array are all set to 0. The typical statements are:

 
   
  
  1. ReDim Types (x+1)

③ assume that you want to change the size of the array and do not want to lose the original data, just to include Preservekeyword in the ReDim statement can be, the typical statement is:

 
   
  
  1. Reda_m presetve Types (10,23)

For multidimensional arrays, you can only change the size of the last dimension when using Preservekeyword. If you change other dimensions, an execution error will occur. Assuming you don't know the current size of a dimension, you can use the Geti~ength function to get it.

(4) VB. NET array for use

In the ' VB 6. 0, the ability to iterate through an array with for each. Like what:

 
   
  
  1. Dim x as Integer
  2. f0r each x in Arrayl
  3. Console. WriteLine (x)
  4. Next

In VB. NET to traverse an array using the For loop and array length. Like what:

 
   
  
  1. Dim I as Work Nteger
  2. f0r I = 0 T0 (Arrayl. LENGTH-1)
  3. Console. WriteLine (Arrayl (1) J
  4. Next I

When working with arrays, it is also important to note that not only does the declarative syntax change, but there is also a very large change in the way it is handled. VB. NET assigns an address space to an array in the stack, and when the parameters of an array type are passed to a method, a reference pass is used instead of a value pass. Here are three ways to pass an array reference to one another:

 
   
  
  1. Dim Arrayl (3,3) as Integer
  2. Dim Array2 as Integer (,)
  3. Redim Array2 (3,3)
  4. Dim Array3 as Integer (,) ={{2,4},{12,29}}

Method one passes an array reference at the same time in two directions, which is typically used to return an array reference to the caller. Method Two and method three pass an array reference from the caller to the implementation of the method. The parameters of method two are declared as one-dimensional arrays, and the parameters in method three are declared as a-dimensional array.

(5) Advanced features of arrays

An array of ① arrays

It is also possible to assemble different types of arrays in the O~ect array. For example, the following code first establishes two arrays, one is an integer type, another is a string type, and then declares an array of type O~ect, and the first two arrays are divided among them.

  
 
  1. Dim I As Integer
  2. Declares an array of type integer
  3. Dim grade () As Integer
  4. For i:0 to 14
  5. Grade (i) =i
  6. NeXt I
  7. Declares an array of type string
  8. Dim name () as String
  9. for I = 0 to
  10. Name (i): "Student" &cstr (i)
  11. Next I
  12. Declares a new array as object, which is used to assemble other arrays
  13. Dim Student (2) As Object
  14. Student (0) =grade
  15. Student (1) =name
  16. Msgbox (Student (0) (2))//display "2"
  17. Msgbox (Student (1) (3))//display "Student 3".

The previous bindings for student (0) and student (1) are used in the preceding code. Note that only the option s~ict is turned off when VB. NET compiler only agrees to use post-bind.

② Arrays and collections

Although a collection pass is often used for manipulating objects, it can also manipulate data types. Under certain conditions, the efficiency is higher than the array. We can compare them in 4 ways.

The collection can be augmented as needed, unlike arrays that require a predetermined size.

Arrays can only hold data types defined at the time of declaration, but different types of data can be stored in the same collection.

The changes to the collection elements are cumbersome and not as convenient as arrays.

Working with collections is slower than arrays, but using collections is the best choice when dealing with smaller sets of dynamic items.

vb.net array definitions Dynamic use of multidimensional arrays

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.