Skip to main content
gamedev.net
🔒 Locked

Array from dotnetzero

Started by foolios Jul 26, 2007 at 7:52 PM 9 replies 1.9k views
Original Post
foolios
foolios
Quote:
new string[] { "North", "East", "South", "West" } [2]
and this:
Quote:
Here‘s a statement that displays one of the four compass points based on the variable dir: Console.Write(new string[] { "North", "East", "South", "West" } [dir]);
Ok, I can see kinda how the value might be South like the book is saying. But where is this being returned to. How do you use this? In the second one, I have no clue what dir is going to result in. How do you use this? Are these some values returned to some method somehow? Maybe there should have been a better example, I dunno.
I rate users. Its just not having much impact as my rating is low...
foolios
foolios
Also,

int[,,] arr = new int[3, 2, 4] {{{ 8, 3, 4, 2}, { 7, 4, 1, 2}}, {{ 2, 7, 3,6},                     { 5, 1, 9, 0}}, {{ 0, 4, 9, 7}, { 3, 9, 8, 5}}};


what would the array look like if it was [3, 2, 4, 1]?
I rate users. Its just not having much impact as my rating is low...
TheTroll
TheTroll
Quote:
Original post by foolios
Quote:

new string[] { "North", "East", "South", "West" } [2]


and this:

Quote:

Here‘s a statement that displays one of the four compass points based on the variable dir: Console.Write(new string[] { "North", "East", "South", "West" } [dir]);


Ok, I can see kinda how the value might be South like the book is saying. But where is this being returned to. How do you use this?

In the second one, I have no clue what dir is going to result in. How do you use this? Are these some values returned to some method somehow? Maybe there should have been a better example, I dunno.


dir is just a variable that will set the direction string.

the first one would just be used like this;
string direction = new string[] { "North", "East", "South", "West" } [2];

The second one could be used like this.
public string GetDirectionString(int dir)
{
return new string[] { "North", "East", "South", "West" } [dir];
}

Now in my opinion, not a very good idea to do it that way. What happens if someone passes in a 4 or higher? Well you are out of range and would get an exception.

A safer way
public enum enumDir{    N,    E,    S,    W}public string GetDirectionString(enumDir dir){     return new string[] { "North", "East", "South", "West" } [(int)dir];}


This way only a valid direction could be passed in. By casting the direction enum to an int you can still use it as an index.

Hope that helps.
theTroll
TheTroll
TheTroll
Quote:
Original post by foolios
Also,

*** Source Snippet Removed ***

what would the array look like if it was [3, 2, 4, 1]?


Actually it would look the same.

Think of how many elements this array has int[1]; 1 correct? How about int[1,1]; still one. How about int[1,1,1]; still one, so int[1,1,1,1]; would still only have one element.

So [3, 2, 4, 1] = [3, 2, 4]..

theTroll
foolios
foolios
Ok, I definitely see how:
string direction = new string[] { "North", "East", "South", "West" } [2];

direction has the reference to the value South on the heap because [2] = the third index of the array where the string object South resides.
Useful to do that I guess so that you can set a default reference upon initialization of the array of objects.


public string GetDirectionString(int dir)
{
return new string[] { "North", "East", "South", "West" } [dir];
}

So here we are passing in an integer that acts like an index to help us pick out what direction out of the array. Cool beans!



what would the array look like if it was [3, 2, 4, 5]?

[Edited by - foolios on July 26, 2007 10:13:37 PM]
I rate users. Its just not having much impact as my rating is low...
TheTroll
TheTroll
Sorry but that is 120 member array so not going to type it out for you.

theTroll
foolios
foolios
what would the array look like if it was [3, 2, 4, 2]?

Sorry edited my post just above your last one.
I rate users. Its just not having much impact as my rating is low...
EternalNewbie
EternalNewbie
I'm too lazy too type up the array. :P
But it may help too think of it as an array that contains another array.
So int [3,2,4,5] is an array with three elements each of which is an array of int [2,4,5].
It might be a helpful excercise to write a simple program which'll display the elements of the array.

Cerasti
Cerasti
int[,,,] arr = new int[3, 2, 4, 2]{{{{8, 3}, {3, 9}, {4, 1}, {2, 6}},{{7, 5}, {4, 4}, {1, 9}, {2, 8}}}, {{{2, 3}, {7, 6}, {3, 6}, {6, 9}},{{5, 5}, {1, 1}, {9, 1}, {0, 4}}},{{ {0, 3}, {4, 2}, {9, 9}, {7, 8}}, {{3, 5}, {9, 6}, {8, 0}, {5, 1}}}};


It gets quite confusing, when the arrays are so big. In the previous array, where there was a number, there are now two numbers. 8 becomes {8, 3} 5 becomes {5, 5} etc.
foolios
foolios
Holy Schamoly! That looks insane.

How did you figure that out?

How did you know where you needed to expand the table?

# of rows, # of columns, # of ? per set of curly braces, # of multidimensions, #?
[3,........2,............4,..............................2,....................2]

(the dots are spaces, used them in an attempt to align the index's with the # symbol above them)


Prolly not that it matters, hopefully that kind of multi dim array won't appear very often.


Thanks for helping with that extra dimension.

[Edited by - foolios on July 27, 2007 6:11:19 PM]
I rate users. Its just not having much impact as my rating is low...
nobodynews
nobodynews
This is where many terms we have available to describe multi-dimensional arrays start to break down. Anyway, to figure out how to initialize an array, start from the right and work your way to the left. So if you have

int[,,,] arr = new int[3, 2, 4, 2]

then the right most size says that you will have an array of two ints. The next digit says that it contains an array of four of the previous type. What is the previous type? An "array of two ints". Then you have two arrays of "an array of four of an array of two ints". Then you have three arrays of "an aray of two of an array of four of an array of two ints". But that's messy and can get confusing.

You can also start from the left an go to the right and picture it like a tree:

                        array                       |  |   |                       |  |   |                      /                     /                    /                   /                  /                 o                / \                       /              /             /        ----o-----        |  |  |  |       /   |  |      /    |  |     /    /        /    /        /     |  o      o / \    / /   \  /   8   3  3   9


Where the end of every branch corresponds to an integer. Alright, so you have the array as the head of this 'tree' and this breaks up into three branches. Each of those three branches break up into two branches. Each of those break up into four branches. And each of those break up into two integers (or, in general, the type of the array).

edit: didn't work correctly the first time or the second time.Third time's the charm. Fourth. Fourth time's the charm. Fifth? Six times!

[Edited by - nobodynews on July 28, 2007 9:19:52 AM]
C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) ,

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.