8.12. Jagged Arrays

Jagged arrays are maintained as arrays of arrays. Unlike rectangular arrays, rows in jagged arrays can be of different lengths. The program in Fig. 8.20 demonstrates the initialization of a jagged array (array1) and the use of nested For...Next loops to traverse the array.

Figure 8.20. Initializing a jagged array.
					1
					' Fig. 8.20: JaggedArray.vb
					2
					' Initializing a jagged array.
					3
					Module JaggedArray
 4
					Sub Main()
 5
					' create jagged array
					
					6
					Dim array1 As Integer()() = New Integer(2)(){}' three rows
					
					7
					array1(0) = New Integer() {1, 2} ' row 0 is a single array 
					
					8
					array1(1) = New Integer() {3} ' row 1 is a single array
					
					9
					array1(2) = New Integer() {4, 5, 6} ' row 2 is a single array
					10
					11        Console.WriteLine("Values in jagged array1 ...

Get Visual Basic 2005 for Programmers: Deitel Developer Series, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.