Solved

jagged array

Posted on 2006-11-15
5
364 Views
Last Modified: 2010-04-23
Hi,

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

thanks in advance

ayha
0
Comment
Question by:ayha1999
  • 2
  • 2
5 Comments
 
LVL 6

Expert Comment

by:riyazthad
Comment Utility
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

by:riyazthad
Comment Utility
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 62

Accepted Solution

by:
Fernando Soto earned 250 total points
Comment Utility
Hi ayha1999;

I hope that this answers your question.

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

by:ayha1999
Comment Utility
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



0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
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
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now