Solved

jagged array

Posted on 2006-11-15
5
373 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
[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
  • Learn & ask questions
  • 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 63

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 63

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Expression Evaluater 3 45
Sending receiving text messages in vb.net 15 62
Code enhancement 4 32
VB.net Excel VSTO Add-in version compatability 2 15
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

756 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