tags:

views:

23

answers:

1

Hi, I have this code, first I thougt it should be the same but i cant acces the info in the same way .. why is this???

    Dim tp(,) As Integer = {{1, 3, 5, 9, 7}, {34, 3, 4, 5, 6}}
    Dim tpo(1)() As Integer
    tpo(0) = New Integer() {1, 3, 5, 9, 7}
    tpo(1) = New Integer() {34, 3, 4, 5, 6}

   For Each s As Integer In tp

        Console.WriteLine(s & ",")

    Next

    For Each di() As Integer In tpo

        For Each di2 As Integer In di

            Console.WriteLine(di2 & ",")
        Next

    Next

The first one I use only one for each and the other one I use two .. why is this different? are noy they 2 dimensional arrays ???

+1  A: 

Your first array declared as

Dim tp(,) As Integer = {{1, 3, 5, 9, 7}, {34, 3, 4, 5, 6}}

declares a 2 dimensional rectangular array with the size of 5X5

Whereas

Dim tpo(1)() As Integer

declares a one dimensional array of integer arrays. This is called a jagged array. Each element in a jagged array can have a different length.

NimsDotNet
Thanks !! and sorry for keep asking, is just a Matter of "filosofy" :) ... I already try both ways, and I got it, but ... they work in the same way, so why to have to ways to do the same thing?, Which one is more efficient?.I see that if a need to use jagged I should use an array of arrays ...Thanks !!
carlos
Performance-wise both should be equal AFAIK, but if you have different amount of data to store under each row, using a jagged array you can save some memory.
NimsDotNet