Sign in to follow this  
mr hooligan

C# Storing Multi Dimensional Arrays in ArrayLists

Recommended Posts

I'm just fooling around with a little idea in c# and I'm having a little trouble.

What I've done is the following.


private ArrayList sectionOne = new ArrayList();

//Method sets up the predefinded rows
private void setupRows() {

//Declare the section array
int[,] section;

section = new int[,] {{0,0,0,0,0},
{0,1,1,1,0},
{0,1,1,1,0},
{0,0,0,9,0}};

sectionOne.Add(section);

section = new int[,] {{0,1,1,1,0}};

sectionOne.Add(section);

section = new int[,] {{0,0,0,3,0},
{0,1,1,1,0},
{0,0,0,0,0}};

sectionOne.Add(section);

}

private void generateSection() {

int[,] section = sectionOne[0] as int[,];

for(int i = 0; i < section.Length; i++) {

for(int j = 0; j < 5; j++) {

print(section[i][j]);

}

}

}

What I'm trying to do is write predefined sections for a mini tile based game and retrieve them randomly. But the generate section doesn't like how I retrieve the arrays? Or maybe how its stored. I'm not too sure on the c# syntax. Haven't used it for a long time.

Any help would be appreciated.

[Edited by - mr hooligan on December 6, 2010 5:52:43 PM]

Share this post


Link to post
Share on other sites
You might want to use List<T> instead of ArrayList. Then you don't have to worry about nasty casting when retrieving elements of the list.

Then you can use Array.GetLength(dimension) to find the length of each dimension of your array.


for (int i=0; i<section.GetLength(0); ++i)
for (int j=0; j<section.GetLength(1); ++j)
...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this