Rectangular Arrays and Jagged Arrays in VB.NET
In this article I will explain you about Rectangular Arrays and Jagged Arrays in VB.NET.
Rectangular Arrays: Rectangular arrays may be single-dimensional or multidimensional but always have a rectangular shape. Rectangular means that the length of each subarray in the same dimension is the same length.
Array types are reference types, so the declaration of an array variable merely sets aside space for the reference to the array. Array instances are actually created via array initializers and array creation expressions, as shown in Listing 20.4.
Listing 20.4: Rectangular Array Creation
' 5 member single-dimensional arrays intialized
Dim shtEmpNo As Short() = New Short(4) {}
' 3 member single-dimensional arrays
Dim intSlNo As Integer() = New Integer() {1, 2, 3}
' 3*2 member two-dimensional arrays
Dim intCount As Integer(,) = New Integer(,) {{1, 2, 3}, {4, 5, 6}}
' 1*3 member three-dimesional arrays
Dim intDec As Integer(,,) = New Integer(9, 19, 29) {}
In fact, in all languages developed for the .NET Framework, rectangular arrays are zero-based array,Listings 20.6 which start assigning array values to elements indexed at zero.
Listing 20.6: Rectangular Array Example
Class [MyClass]
Private Shared Sub Main()
Dim intDec As Integer() = New Integer(4) {}
For i As Integer = 0 To intDec.Length - 1
intDec(i) = i * 10
Next
For i As Integer = 0 To intDec.Length - 1
Console.WriteLine("intDec[{0}] = {1}", i, intDec(i))
Next
End Sub
End Class
Jagged Arrays
Jagged arrays are arrays of arrays. The syntax for a jagged array uses the square brackets after the type declaration for each dimension of the array:
Listing 20.7 compares rectangular and jagged arrays.
Listing 20.7: Rectangular and Jagged Arrays
'single-dimensional rectangular arrays
Dim r1 As Integer() = New Integer() {1, 2, 3}
'two-dimensional rectangular arrays
Dim r2 As Integer(,) = New Integer(,) {{1, 2, 3}, {4, 5, 6}}
'three-dimesional rectangular arrays
Dim r3 As Integer(,,) = New Integer(9, 19, 29) {}
'"jagged" array: array of (array of integer)
Dim j2 As Integer()() = New Integer(2)() {}
j2(0) = New Integer() {1, 2, 3}
j2(1) = New Integer() {1, 2, 3, 4, 5, 6}
j2(2) = New Integer() {1, 2, 3, 4, 5, 6, 7, 8, 9}
The code in Listing 20.7 includes various expressions for creating both rectangular and jagged arrays. The variables r1, r2, and r3 are rectangular arrays, and the variable j2 is a jagged array.
Rectangular arrays always have a rectangular shape. For example, the length of r3's three dimensions are 9, 19, and 29, and it is easy to see that this array contains 9 x 19 x 29 elements.
The variable j2 is a jagged arrayâ€"an array of an array of integers, or a single-dimensional array of type integer(). Each of these integer() variables can be initialized individually, and this allows the array to take on a jagged shape. Listing 20.7 gives each of the integer() arrays a different length. Specifically, the length of j2[0] is 3, the length of j2[1] is 6, and the length of j2[2] is 9. Listing 20.8 illustrates how to create and loop through a jagged array.
Listing 20.8: Creating and Outputting Jagged Arrays
' Jagged Array example
Public Class MyArrayc2
Public Shared Sub Main()
Dim JaggedArray As Integer()() = New Integer(3)() {}
JaggedArray(0) = New Integer(2) {}
JaggedArray(1) = New Integer(1) {}
JaggedArray(2) = New Integer(4) {}
JaggedArray(3) = New Integer(3) {}
Console.WriteLine("Enter the numbers for Jagged Array")
For i As Integer = 0 To JaggedArray.Length - 1
For x As Integer = 0 To JaggedArray(i).Length - 1
Dim st As [String] = Console.ReadLine()
Dim num As Integer = Int32.Parse(st)
JaggedArray(i)(x) = num
Next
Next
Console.WriteLine("")
Console.WriteLine("Printing the Elements")
For x As Integer = 0 To JaggedArray.Length - 1
For y As Integer = 0 To JaggedArray(x).Length - 1
Console.Write(JaggedArray(x)(y))
Console.Write(vbNullChar)
Next
Console.WriteLine("")
Next
Console.ReadLine()
End Sub
End Class
Output Window
Conclusion
Hope this article would have helped you in understanding Rectangular Arrays and Jagged Arrays in VB.NET.