Solved

jagged array

Posted on 2006-11-15
5
370 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
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

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

Accepted Solution

by:
Fernando Soto earned 250 total points
ID: 17950528
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
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?

thanks in advance.

ayha



0
 
LVL 62

Expert Comment

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

Fernando
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

772 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