Array Lab Task

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

ARRAY LAB TASK

PROGRAM# 1
Module Module1
Sub Main()
Dim array As Integer() = New Integer(4) {1, 2, 3, 4, 5}
Console.WriteLine(array(0))
Console.WriteLine(array(1))
Console.WriteLine(array(2))
Console.WriteLine(array(3))
Console.WriteLine(array(4))
Console.WriteLine("Press Enter Key to Exit..")
Console.ReadLine()
End Sub
End Module

Program_2
Module Module1
Sub Main()
Dim array As Integer() = New Integer(4) {1, 2, 3, 4, 5}
For i As Integer = 0 To array.Length - 1
Console.WriteLine(array(i))
Next
Console.WriteLine("Press Enter Key to Exit..")
Console.ReadLine()
End Sub
End Module

Visual Basic Access Array Elements with Foreach Loop


Program_3
Module Module1
Sub Main()
Dim array As Integer() = New Integer(4) {1, 2, 3, 4, 5}
For Each i As Integer In array
Console.WriteLine(i)
Next
Console.WriteLine("Press Enter Key to Exit..")
Console.ReadLine()
End Sub
End Module

Visual Basic Array Types


In visual basic, we have a different type of arrays available, those are

• Single-Dimensional Arrays
• Multi-Dimensional Arrays
• Jagged Arrays

Program_4
Module Module1
Sub Main()
Dim arr As Integer() = New Integer(4) {1, 4, 2, 3, 5}
Console.WriteLine("---Initial Array Elements---")
For Each i As Integer In arr
Console.WriteLine(i)
Next
Array.Sort(arr)
Console.WriteLine("---Elements After Sort---")
For Each i As Integer In arr
Console.WriteLine(i)
Next
Array.Reverse(arr)
Console.WriteLine("---Elements After Reverse---")
For Each i As Integer In arr
Console.WriteLine(i)
Next
Console.WriteLine("Press Enter Key to Exit..")
Console.ReadLine()
End Sub
End Module
Visual Basic Multi-Dimensional Array Declaration
' Two Dimensional Integer Array
Dim intarr As Integer(,) = New Integer(2, 1) {{4, 5}, {5, 0}, {3, 1}}
' Two Dimensional Integer Array without Dimensions
Dim intarr1 As Integer(,) = New Integer(,) {{4, 5}, {5, 0}, {3, 1}}
' Three Dimensional Array
Dim array3D As Integer(,,) = New Integer(1, 1, 2) {{{1, 2, 3}, {4, 5, 6}},
{{7, 8, 9}, {10, 11, 12}}}
' Three Dimensional Array without Dimensions
Dim array3D1 As Integer(,,) = New Integer(,,) {{{1, 2, 3}, {4, 5, 6}},
{{7, 8, 9}, {10, 11, 12}}}

Visual Basic Access Multidimensional Array


Elements
Program_5

' Two Dimensional Array


Dim arr2D As Integer(,) = New Integer(2, 1) {{4, 5}, {5, 0}, {3, 1}}
' Three Dimensional Array
Dim array3D As Integer(,,) = New Integer(1, 1, 2) {{{1, 2, 3}, {4, 5, 6}},
{{7, 8, 9}, {10, 11, 12}}}
Console.WriteLine(arr2D(1, 0)) ' 5
Console.WriteLine(array3D(1, 1, 1)) ' 11

Visual Basic Multidimensional Array Example


Following is the example of using multidimensional arrays in visual basic programming language to
represent the elements in an array with multiple dimensions.

Program_6

Module Module1
Sub Main()
' Two Dimensional Array
Dim array2D As Integer(,) = New Integer(2, 1) {{4, 5}, {5, 0}, {3,
1}}
' Three Dimensional Array
Dim array3D As Integer(,,) = New Integer(1, 1, 2) {{{1, 2, 3}, {4,
5, 6}}, {{7, 8, 9}, {10, 11, 12}}}
Console.WriteLine("---Two Dimensional Array Elements---")
For i As Integer = 0 To 3 - 1
For j As Integer = 0 To 2 - 1
Console.WriteLine("a[{0},{1}] = {2}", i, j, array2D(i, j))
Next
Next
Console.WriteLine("---Three Dimensional Array Elements---")
For i As Integer = 0 To 2 - 1
For j As Integer = 0 To 2 - 1
For k As Integer = 0 To 3 - 1
Console.WriteLine("a[{0},{1},{2}] = {3}", i, j, k, arr
ay3D(i, j, k))
Next
Next
Next
Console.WriteLine("Press Enter Key to Exit..")
Console.ReadLine()
End Sub
End Module

Visual Basic Jagged Arrays


' Jagged Array with Single Dimensional Array
Dim jarray As Integer()() = New Integer(1)() {}
' Jagged Array with Two Dimensional Array
Dim jarray1 As Integer()(,) = New Integer(2)(,) {}

Visual Basic Jagged Array Initialization


' Jagged Array with Single Dimensional Array
Dim jarray As Integer()() = New Integer(2)() {}
jarray(0) = New Integer(4) {1, 2, 3, 4, 5}
jarray(1) = New Integer(2) {10, 20, 30}
jarray(2) = New Integer() {12, 50, 60, 70, 32}
' Jagged Array with Two Dimensional Array
Dim jarray1 As Integer()(,) = New Integer(2)(,) {}
jarray1(0) = New Integer(1, 1) {{15, 24}, {43, 54}}
jarray1(1) = New Integer(,) {{11, 12}, {13, 14}, {25, 26}}
jarray1(2) = New Integer(3, 2) {}
' Initializing an Array on Declaration
Dim jarray2 As Integer()() = New Integer()() {New Integer() {1, 2, 3, 4,
5}, New Integer() {98, 56, 45}, New Integer() {32}}
Visual Basic Access Jagged Array Elements

Visual Basic Jagged Array Example


Following is the example of using jagged arrays in visual basic programming language to represent
the arrays as elements of the other array with single or multiple dimensions.

Program_7
Module Module1
Sub Main()
' Jagged Array with Single Dimensional Array
Dim jarray As Integer()() = New Integer(2)() {}
jarray(0) = New Integer(4) {1, 2, 3, 4, 5}
jarray(1) = New Integer(2) {10, 20, 30}
jarray(2) = New Integer() {12, 50, 60, 70, 32}
Console.WriteLine("---Jagged Array with Single Dimensional Element
s---" & vbLf)
For i As Integer = 0 To jarray.Length - 1
Console.Write("Element[{0}]: ", i)
For j As Integer = 0 To jarray(i).Length - 1
Console.Write("{0}{1}", jarray(i)(j), If(j = (jarray(i).Le
ngth - 1), "", " "))
Next
Console.WriteLine()
Next
' Jagged Array with Two Dimensional Array
Dim jarray1 As Integer()(,) = New Integer(1)(,) {}
jarray1(0) = New Integer(1, 1) {{15, 24}, {43, 54}}
jarray1(1) = New Integer(,) {{11, 12}, {13, 14}, {25, 26}}
Console.WriteLine(vbLf & "---Jagged Array with Mult-Dimensional El
ements---" & vbLf)
For i As Integer = 0 To jarray1.Length - 1
Console.Write("Element[{0}]: ", i)
For j As Integer = 0 To jarray1(i).GetLength(0) - 1
Console.Write("{")
For k As Integer = 0 To jarray1(i).GetLength(1) - 1
Console.Write("{0}{1}", jarray1(i)(j, k), If(k = (jarr
ay1(i).GetLength(1) - 1), "", " "))
Next
Console.Write("{0}{1}", "}", If(j < jarray1.GetLength(0)
, ", ", ""))
Next
Console.WriteLine()
Next
Console.WriteLine(vbLf & "Press Enter Key to Exit..")
Console.ReadLine()
End Sub
End Module

You might also like