Jump to content
  • Advertisement
Sign in to follow this  
shane1985

[.net] Multidimensional Arrays

This topic is 4873 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is there any easy way to use dynamic multidimensional arrays in C#? I know you can make arrayLists of arrayLists but that would involve a bunch of casts and unweildy code. Is there an easier way to do it? -Shane

Share this post


Link to post
Share on other sites
Advertisement

int j = 10;
int[,] fa = new int[j,j];

or
int[][] ra = new int[j][];
for(int i = 0; i < ra.Length; ++i) {
ra = new int[j];
}



take your pick, jagged vs square.

Share this post


Link to post
Share on other sites
Alright, I was also wondering how to resize a multidimensional array (square).
It looks like the array class only works for single dimensional arrays.

-Shane (.NET rookie)

Share this post


Link to post
Share on other sites
A jagged array is an array of arrays. So, you can still use the Array class on it and its elements.

There's also the System.Collections.ArrayList class, which is a one-dimensional array that resizes itself. If you have an ArrayList whose elements were all ArrayLists, that will work as a multidimensional, automatically resized array.

Share this post


Link to post
Share on other sites
J/W, but couldn't you also create a custom collection that works like an ArrayList, but is implemented with your custom type? That way you could return a double or MyClass instead of System.Object.

I'm not really familiar with System.Collections, though, so I might be wrong...

Here's an article that shows what I mean...

Share this post


Link to post
Share on other sites
Quote:
Original post by Del Snd of Thndr
J/W, but couldn't you also create a custom collection that works like an ArrayList, but is implemented with your custom type? That way you could return a double or MyClass instead of System.Object.

You don't need to write it because .NET 2.0 already includes it. They are called generics and they look a little bit like templates in C++.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!