Arrays Declaring and Using Arrays
Arrays Declaring and Using Arrays
NET Arrays
An array is a linear data structure that is a collection of data elements of the same type stored on
a contiguous memory location. Each data item is called an element of the array. It is a fixed size of
sequentially arranged elements in computer memory with the first element being at index 0 and the last
element at index n - 1, where n represents the total number of elements in the array.
The following is an illustrated representation of similar data type elements defined in the VB.NET array data
structure.
In the above diagram, we store the Integer type data elements in an array starting at index 0. It will continue
to store data elements up to a defined number of elements.
In the above declaration, array_name is the name of an array, and the Data_Type represents the type of
element (Integer, char, String, Decimal) that will to store contiguous data elements in the VB.NET array.
Furthermore, we can also initialize and declare an array using the following ways, as shown below.
Let's create a program to add the elements of an array in VB.NET programming language.
num_Array.vb
1. Imports System
2. Module num_Array
3. Sub Main()
4. Dim i As Integer, Sum As Integer = 0
5. 'In VB.NET the size of an array is n+1
6. 'Declaration and Initialization of marks() array
7. Dim marks() As Integer = {58, 68, 95, 50, 23, 89}
8. Console.WriteLine(" Marks in 6 Subjects")
9. For i = 0 To marks.Length - 1
10. Console.WriteLine(" Marks {0}", marks(i))
11. Sum = Sum + marks(i)
12. Next
13. Console.WriteLine(" Grand total is {0}", Sum)
14.
15. Console.WriteLine(" Press any key to exit...")
16. Console.ReadKey()
17. End Sub
18. End Module
Output:
In the above program, we create an integer array with name marks() and define a For loop to access each
item of the array marks.
Input_array.vb
1. Imports System
2. Module Input_array
3. Sub Main()
4. 'Definition of array
5. Dim arr As Integer() = New Integer(5) {}
6. For i As Integer = 0 To 5
7. Console.WriteLine(" Enter the value for arr[{0}] : ", i)
8. arr(i) = Console.ReadLine() ' Accept the number in array
9. Next
10. Console.WriteLine(" The array elements are : ")
11. ' Definition of For loop
12. For j As Integer = 0 To 5
13.
14. Console.WriteLine("{0}", arr(j))
15. Next
16.
17. Console.WriteLine(" Press any key to exit...")
18. Console.ReadKey()
19. End Sub
20. End Module
Output:
Multidimensional Array
In VB.NET, a multidimensional array is useful for storing more than one dimension in a tabular form, such as
rows and columns. The multidimensional array support two or three dimensional in VB.NET.
MultidimenArray.vb
1. Imports System
2. Module MultidimenArray
3. Sub Main()
4. ' Definition of 2 Dimensional Array
5. Dim intArray(,) As Integer = {{5, 4}, {3, 2}, {4, 7}, {4, 5}}
6.
7. ' Definition of 3 Dimensional Array
8. Dim threeDimen(,,) As Integer =
9. {{{1, 3, 2}, {2, 3, 4}},
10. {{5, 3, 6}, {3, 4, 5}},
11. {{1, 2, 2}, {5, 2, 3}}}
12.
13. Console.WriteLine(" Two Dimensional Arraye in VB.NET are")
14. For i As Integer = 0 To 3
15. For j As Integer = 0 To 1
16. Console.WriteLine("intArray[{0}, {1}] = {2}", i, j, intArray(i, j))
17. Next j
18. Next i
19.
20. Console.WriteLine(" Three Dimensional Arraye in VB.NET are")
21. For i As Integer = 0 To 2 - 1
22. For j As Integer = 0 To 2 - 1
23. For k As Integer = 0 To 4
24. Console.WriteLine("intArray[{0}, {1}, {2}] = {3}", i, j, k, threeDimen(i, j, k))
25. Next k
26. Next j
27. Next i
28.
29. Console.WriteLine(" Press any key to exit...")
30. Console.ReadKey()
31. End Sub
32. End Module
Output:
Fixed Size Array
In VB.NET, a fixed- size array is used to hold a fixed number of elements in memory. It means that we have
defined the number of elements in the array declaration that will remain the same during the definition of
the elements, and its size cannot be changed. For example, we need to hold only 5 names in an array; it can
be defined and initialized in the array such as,
The above representation of the fixed array is that we have defined a string array names 0 to 4, which stores
all the elements in the array from 0 to index 4.