# Multidimensional array

Hi,

Could anyone pls. explain what a multidimentional array is and where it can be used? pls. give a me sample.

ayha
LVL 7
###### Who is Participating?

x

Commented:
An array with size (4, 4) can hold 5 rows and 5 columns worth of data, so 25 total things (don't forget VB's psychotic syntax where you size the array with its maximum element, not the length of the array, so if you give a "size" of 4 you really have 5 elements to play with).

An array with size (5, 5, 5) can hold 5 rows, 5 columns, and be 5 levels deep, so 5x5x5 = 125 total elements.

If you declare it without filling numbers, such as with (,) you're saying "this is a 2D array but I don't know how big it'll be."  This is useful when declaring functions:

Public Sub DoStuffToBigArray(ByVal array As Integer(,))
' ...
End Sub

Of course, when you instantiate the array you must give it a size, but there are plenty of places in code where you'll just be expecting an array to exist already, and while you certainly care about the number of dimensions you don't care about the number of elements in each dimension.
0

Commented:
Normally we think of a one-dimensional array as sort of a single column on a spreadsheet:

0.  Element 1
1.  Element 2
2.  Element 3

A two-dimensional array is more like the whole spreadsheet -- rows and columns.  In intro programming courses a typical assignment covering 2D arrays is to create a Bingo game.  To create such a game, you'd need to know which numbers were on the Bingo card in which rows and columns.  You could, of course, create some sort of scheme using a 1D array where the first few elements are row 1, then the next few elements are row 2, but that is basically a really good way of giving yourself a headache.

Instead you could create a 2D array like so:
Dim card As Integer(4, 4)

' Now card(0, 0) is the upper left thing, and card(2, 2) is the free space in the center of the card.

I should apologize for using such a clichÃ© example, but I guess enough years spent teaching intro programming assignments leaves a certain indelible mark when it comes to 2D array examples.  (-:
0

Commented:
I should add an additional note: it's entirely possible to create a 3 or 4 or 27 dimension array by exactly the same procedure and the computer won't complain in the least about implementing it.  As soon as you figure out how to wrap your mind around 27 dimensions, though, be sure to let the rest of us know.  I think some Physicists in particular would be interested to know how you did it.  (-;

A 3D array is rare, but at least still imaginable.  To expand my own example above you might want an "array of Bingo cards."  What's an array of Bingo cards?  It's an array of 2D arrays or (wait for it...) a 3D array.  Then you might access cards(19, 0, 0) to see the free space on the 20th card in the list.

As a rule if you're contemplating a 3D array (or, often, even a 2D array) you may not be properly exploiting Object-Oriented programming.  Of course, there are still cases where it's perfectly appropriate (the above not actually being one of them).
0

Author Commented:
HI,

what is this (4, 4) means or (4,4,4) or (,,,)means. I know that single dimesion holds only one column, 2D menas two columns and 3D means 3 columns like that?

ayha
0

Commented:
Yep, that's precisely it.  The first number is the row, the second the column, the third represents the third dimension, and the 27th number will represent the 27th dimension.
0

Author Commented:
i think here (4,4) can the follwoing data;

1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4

and what about (4,4,4)? how many rows and columns and rows can hold in it? if I declare it like (,)or (,,) or (,,,) what does ',' means and how many rows and columsn can it hold it?

ayha
0

Commented:
read Votey's description, i dont think you read and thought it through.
anyway think of it with this example:

you have 1 person that you want to store his daily habits in an array. you would use a 1 dimensional array
(1) = Wake Up
(2) = Go to bathroom
(3) = Have Lunch

imagine you now need to store habits for many people.
Person's 1 array would be
(1, 1) = Wake Up
(1, 2) = Go to Bathroom
...
person's 2 array would be
(2, 1) = Wake Up
(2, 2) = Eat Breakfast
...
person's 89 array would be
(89, 1) = Wake Up
(89, 2) = Watch TV
...

if you want to add a 3rd dimension, for example Country... A 3 dimensional example would be:
Person 1, living in USA, habits:
(1,1,1) = Wake Up
(1,1,2) = Go to the Bathroom
...
Person 2, living in USA habits:
(1,2,1) = Wake Up
(1,2,2) = Eat Breakfast
...
now, person 1, living in Iceland habits
(2,1,1) = Wake Up
(2,1,2) = Go to Bathroom

you see how it gets complicated now. you could add 4th dimension to represent Planets.
Person 1, living in USA of Planet Earth, habits would be:
(1,1,1,1) = Wake Up
(1,1,1,2) = Go to Bathroom
...

Person 89, living in Iceland of Planet Mars would be:
(2,2,89,1) = Wake Up
(2,2,89,2) = Eat Breakfast
0

Commented:
Correction.  I wrote, "An array with size (5, 5, 5) can hold 5 rows, 5 columns, and be 5 levels deep, so 5x5x5 = 125 total elements."

I of course meant, "An array with size (4, 4, 4) can hold 5 rows, 5 columns, and be 5 levels deep, so 5x5x5 = 125 total elements."
0

Author Commented:
Hi,

Let's take two situations. in first one I am retrieving 1000 rows and 10 colums then how can delcare array for this purpose?

second one: I don't no. of rows but 10 columns (or even dotn't know no. of colums) then how can I delcare array?

ayha
0

Commented:
Well, I've told you three things that should make it easy to answer that first question.
1.  The first number in the list is the number of rows
2.  The second number in the list is the number of columns
3.  The numbers you give are the maximum indices, not the number of elements you actually want (so subtract 1).

0

Author Commented:
Hi  VoteyDisciple,

you means if I declare Arr(,,) means it can unlimited rows and columns, right?

ayha
0

Commented:
No, there is no such thing as an array with unlimited space.  All arrays take up a fixed amount of memory.

If you declare a REFERENCE to an array like Integer(,,)  it means "When you allocate memory for this, it has to be a three-dimensional array of integers, but I don't care how big it is."

Thus the following two lines are both valid:
Dim array1 As Integer(,,) = New Integer(3,6,1938)
Dim array2 As Integer(,,) = New Integer(4, 400, 40)

Again, this is generally useful only when you are declaring function/sub parameters when you have no need to allocate new memory, you just want to get passed-in an existing array.
0

Author Commented:
hi  VoteyDisciple,

suppose a query "select id, name, city from table1" returns many rows.How can I declare an array here to hold all rows and three columns?

ayha
0

Commented:
Figure out how many rows it returns, using whatever function tells you that after running the query, and declare the array with that size.  There's absolutely no rule that array sizes need to be literals in your code; you can allocate memory based on a variable just as easily as based on a literal.  It's just that once you've allocated the memory its size doesn't change.

Alternatively, use a structure like ArrayList that allows elements to be inserted/removed dynamically.
0

Author Commented:
hi,

is it possible to use multidimension with arraylist? how can I use it for storing unlimited rows and 3 colums from an sql query?

sorry, Ihave no previous knowledge or experince about multidimensionla array.

ayha
0

Commented:
It sounds like you're trying to build an array of rows selected by a query, right?  This is precisely a situation where considering a multi-dimensional array means you're overlooking a much better OO solution.

Create a class to represent one row of data, and then build a single ordinary, easy-to-understand and thus totally uninteresting 1D array whose elements are instances of that class.

Especially since database queries tend to return heterogeneous data (strings, integers, floating point numbers -- each column has its own type) and arrays are inherently homogenous.  An OO solution to this would be far more appropriate.
0

Author Commented:
i.e. this type of query result cannot stored in array, right? I tried single dimension array to store unlimited number of rows with single column and succeeded. what is the difference between an array and an arraylist? is it possible to do multidimension with an array list?

I incremented points.

ayha
0

Commented:
You'll want to double-check your "unlimited number of rows" code, since I repeat that such a structure does not exist.  An ArrayList certainly behaves that way, but there's no basic datatype that can allocate limitless memory in one shot.  If you're unfamiliar with ArrayList you might want to look it up in the documentation -- it's pretty straightforward.  It acts like an array, but is in reality a full-featured class with Insert and RemoveAt sorts of methods -- the kinds of things you're probably used to using in a graphical ListBox control.

I'll repeat, however, that if you're looking to store a database row, a multidimensional array is fundamentally the wrong structure do it.  Create a class, and use that instead.
0

Author Commented:
is it possible to do multidimension with an arraylist? you mean I want to use the query result Imentioned earlier I must decleare the size of the array first, like (100000,3) etc. right?

ayha

0

Commented:
No, each element of an ArrayList is a single object.  That object could be an ArrayList itself, or even an array; for that matter you could make an array of ArrayLists, but unless you've got a really sound reason for doing it that's just adding confusion unnecessarily.

Seriously, why are you opposed to an OO solution?
0

Author Commented:
I mean this  (100000,3) declaration in an array not in an arralylist.

ayha
0

Author Commented:
could u explain the last 4 in (4,4,4)?

ayha
0

Commented:
Going back to the spreadsheet example, the last dimension might be like having a whole bunch of spreadsheets stacked up together.  So you've got rows and columns on each sheet, and then a whole stack of sheets.
0

Author Commented:
what is the maximum number of dimension allowed in an array?

ayha
0

Commented:
That's bounded by the amount of contiguous memory your system can allocate.  I do not know of any limit fixed by the .NET architecture; I believe the allocation goes straight back to the OS and you either get what you requested or it dies.

If you're worried about maxing out a system's memory with a single array I'd suggest a radically different solution is clearly in order.  Unless you're really doing some form of processing that just requires a massive amount of data simultaneously.
0

Author Commented:
in the follwoing example 4 rows and three columns are declared. could u pls. modify the line below
Dim rectangularArray As Integer(,) = _
{{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {9, 10, 11}}

if I add a thrird dimension after 'Const columnsUB As Integer = 3'?

Shared Sub Main()
Const rowsUB As Integer = 4
Const columnsUB As Integer = 3

Dim rectangularArray As Integer(,) = _
{{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {9, 10, 11}}

Dim i As Integer
For i = 0 To rowsUB - 1
Dim j As Integer
For j = 0 To columnsUB - 1
Console.WriteLine( _
"rectArr[{0},{1}] = {2}", _
i, j, rectAr(i, j))
Next j
Next i
End Sub

ayha
0

Commented:
Well, if {0, 1, 2} defines a 1D array, and {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {9, 10, 11}} defines a 2D array, what would it take to define a 3D array?
0

Author Commented:
the spread sheet example was not clear to me that's why I am asking what the third, fourgh number in (,,,) is.

ayha
0

Commented:
Okay, try this:

Draw out, on a piece of paper, a grid representing a 2D array.  This, naturally, would have rows numbered 0 to, say, 4 and columns numbered from 0 to, let's say, 6

That's an ordinary (4, 6) array.

Now, on a second piece of paper draw another, identical 5x7 grid numbered the same way -- rows 0 to 4, columns 0 to 6.

Number the first piece of paper (the whole piece of paper) page 0.  Number the second piece of paper page 1.

Now you have a (4, 6, 1) array.

As I suggested, it gets extremely difficult to mentally grasp a 4D array since we live essentially in a 3D world.  Again, any time you're stretching that far into multidimensional arrays in .NET you're probably (not definitely, but most probably) doing something wrong.
0

Author Commented:
so in this example the last 1 means two papers, right? to assign data to it

for the first page:
(0, 0, 0 ) = ''

second page:
(0, 0, 1) = ''

right?

ayha
0

Commented:
Correct.  That would store a value in the upper-left box on the first page (number 0) and then store value in the upper-left box on the second page (number 1).
0

Author Commented:
Thank u very much help.

I will give the max points.

thanks once again.

ayha
0

Author Commented:
0

Commented:
my god i have never seen such a long thread before on such a trivial matter.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.