Solved

# jagged array

Posted on 2006-11-15
Medium Priority
396 Views
Hi,

what is a jagged array? pls. explain its usage with a simple and clear example.

ayha
0
Question by:ayha1999
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2
• 2

LVL 6

Expert Comment

ID: 17950037
jagged array are array of arrays. Each element can hold array of various size.

for ex

Dim ar1() As Integer = {1, 2, 3}
Dim ar2() As Integer = {4, 5}
Dim allarray()() As Integer = {ar1,ar2}

0

LVL 6

Expert Comment

ID: 17950076
you can do like this also

Dim vehicle(1)() as String

vehicle(0)=New String(){"Red","blue","Green"}
vehicle(1)=New String(){"Car","bus","truck","bike"}

here vehicle is array size 2. First element is an array of 3 members and second element is an array of 4 members.

0

LVL 64

Accepted Solution

Fernando Soto earned 1000 total points
ID: 17950528
Hi ayha1999;

Some sample code

Dim JaggedArray()() As String = {New String() {"Alice", "Albert", "Allen"}, _
New String() {"Bob", "Bill", "Brenda", "Bert"}, _
New String() {"Zac"}}

For idx As Integer = 0 To JaggedArray(idx).Length - 1
For idx2 As Integer = 0 To JaggedArray(idx).Length - 1
Console.WriteLine(JaggedArray(idx)(idx2))
Next
Next

JaggedArray         C0         C1          C2            C3
R0                  Alice      Albert      Allen
R1                  Bob      Bill       Brenda         Bert
R2                  Zack

A jagged array is also known by the name of an Array of Arrays. What this means is that each row in the array is defined by another array. So looking at the table above row R0 has an array that contains 3 elements. Row R1 has another array of 4 elements and row R2 has another array with 1 element in it. So lets say that you want to access the name Breda in the above jiggered array then you would do the following

Dim name As String = JaggedArray(1)(2)

So what you have done is to access the 1 element in the first array and the 3rd element in the second array. So the question is what does this buy you and the answer is it saves you space. In the above array you only needed a total of 8 elements to define the array. If you were to define this with a regular array you would have needed 12 elements. So if you needed to define an array that could have a max of 1000 values in a row with a min number of values of 1, and you had 1000 rows then in a regular array you will need enough memory to hold 1,000,000 values. But if not all the rows were filled then the memory would be wasted. With a jagged array you only use what you need and no more.

Fernando
0

LVL 7

Author Comment

ID: 17988492
Hi Fernando,

pls. explain the follwoing:

Dim JaggedArray()() As String means we are declaring three arrays right?

need to menioned columns and row as in regluar arrays?

if I want to retrive only items from the second one (()()) how can retrieve it?

if Iwant to store only items ( from a datareader) in the third array, how can I do that?

ayha

0

LVL 64

Expert Comment

ID: 17993534
Hi Ayha;

To your question: "Dim JaggedArray()() As String means we are declaring three arrays right?", In the sample I gave in my post there are actually 4 arrays. The first array has 3 elements in it as denoted in my table below with R0 - R2. Each one of the elements itself holds another array of different sizes also shown in the JaggedArray table Below as C0 - C3.

In the table below any location with a <> in it is not an array element. Therefore row R0 has an array of length 3, row R1 has an array of length 4, and row R2 has an array of length of 1,
JaggedArray        C0        C1         C2          C3
R0                      Alice     Albert    Allen       <>
R1                      Bob      Bill        Brenda    Bert
R2                      Zack     <>       <>          <>

To your question, "if I want to retrieve only items from the second one (()()) how can retrieve it?", Let call row R1 as the second one. To access that part of the jagged array JaggedArray(R1)(Cx) therefore to access al the elements of R1 you could do it like this

For idx As Integer = 0 To JaggedArray(1).GetUpperBound(0)
Console.WriteLine(JaggedArray(1)(idx))
Next

In this part of the For statement JaggedArray(1).GetUpperBound(0), JaggedArray(1) looks at row R1 and GetUpperBound(0) gets the upper bound of the only array.

To your question, "if I want to store only items ( from a datareader) in the third array, how can I do that?", As you would in a normal array.

For idx As Integer = 0 To JaggedArray(2).GetUpperBound(0)
Next

Fernando
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…