C# Storing Multi Dimensional Arrays in ArrayLists

Started by
1 comment, last by Nypyren 13 years, 4 months ago
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[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]
Advertisement
Don't worry about this post. I spotted how stupid I am :) Must have been tired last night.
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)    ...

This topic is closed to new replies.

Advertisement