Link to home
Start Free TrialLog in
Avatar of ayha1999
ayha1999

asked on

jagged array

Hi,

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

thanks in advance

ayha
Avatar of riyazthad
riyazthad

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}

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.

ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ayha1999

ASKER

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?

thanks in advance.

ayha



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)
            JaggedArray(2)(idx) = datareader
        Next

Fernando