0% found this document useful (0 votes)
16 views

Arrays Declaring and Using Arrays

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Arrays Declaring and Using Arrays

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

VB.

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.

Declaration of VB.NET Array


We can declare an array by specifying the data of the elements followed by parentheses () in the VB.NET.

1. Dim array_name As [Data_Type] ()

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.

Now, let us see the example to declare an array.

1. 'Store only Integer values


2. Dim num As Integer() or Dim num(5) As Integer
3. 'Store only String values
4. Dim name As String() or Dim name(5) As String
5. ' Store only Double values
6. Dim marks As Double()

Initialization of VB.NET Array


In VB.NET, we can initialize an array with New keyword at the time of declaration. For example,

1. 'Declaration and Initialization of an array elements with size 6


2. Dim num As Integer() = New Integer(5) { }
3. Dim num As Integer() = New Integer(5) {1, 2, 3, 4, 5, 6}
4. Initialize an array with 5 elements that indicates the size of an array
5. Dim arr_name As Integer() = New Integer() {5, 10, 5, 20, 15}
6. Declare an array
7. Dim array1 As Char()
8. array1 = New Char() {'A', 'B', 'C', 'D', 'E'}

Furthermore, we can also initialize and declare an array using the following ways, as shown below.

1. Dim intData() As Integer = {1, 2, 3, 4, 5}


2. Dim intData(5) As Integer
3. Dim array_name() As String = {"Peter", "John", "Brock", "James", "Maria"}
4. Dim misc() as Object = {"Hello friends", 16c, 12ui, "A"c}
5. Dim Emp(0 to 2) As String
6. Emp{0} = "Mathew"
7. Emp(1) = " Anthony"
8. Emp(2) = "Prince"

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 number in VB.NET Array


Let's create a program to take input values from the user and display them in VB.NET programming
language.

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.

Declaration of Multidimensional Array

1. Declaration of two-dimensional array


2. Dim twoDimenArray As Integer( , ) = New Integer(3, 2) {}
3. Or Dim arr(5, 3) As Integer
4. Representation of Three Dimensional array
5. Dim arrThree(2, 4, 3) As Integer
6. Or Dim arr1 As Integer( , , ) = New Integer(5, 5, 5) { }

In the above representation of multidimensional, we have created 2-dimensional array twoDimenArray


with 3 rows and 2 columns and 3-dimensional array with three dimensions 2, 4, and 3.

Initialization of Multidimensional Array

The following ways to initialize the multidimensional array:

1. ' Initialization of Two Dimensional Array


2. Dim intArray As Integer( , ) = New Integer( 3, 2) { {4, 5}, {2, 3}, {6, 7} }
3. Dim intArray( , ) As Integer = { {5, 4}, {3, 2}, {4, 7} }
4. ' Initialization of Three Dimensional Array
5. Dim threeDimen(3, 3, 2 ) As Integer = { {{1, 3, 2}, {2, 3, 4}}, {{5, 3, 6}, {3, 4, 5}}, {{1, 2, 2}, {5, 2, 3} }}

Multidimensional Array Example

Let's create a program to understand the multidimensional array.

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,

1. Dim names( 0 to 4) As String


2. names(0) = "Robert"
3. names(1) = "Henry"
4. names(2) = "Rock"
5. names(3) = "James"
6. names(4) = "John"

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.

You might also like