# jagged array

Hi,

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

ayha
LVL 7
###### Who is Participating?

x

RetiredCommented:
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

Commented:
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

Commented:
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

Author Commented:
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

RetiredCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.