Jump to content
  • Advertisement
Sign in to follow this  
FantasyVII

Convert 1D array to 2D array C#

This topic is 2159 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

Hello everyone.

I have the following file that looks like this:-

1,2,3,4,5,6,7,8,9,
10,11,12,13,14,15,
16,17,18,19,20,21,


now I'm reading it and I stored all the data into 1D string array. Then I parse that 1D string array to 1D Int array. However I need to copy the data to 2D array. I have been trying to do it but I can't seem to get my head around it.

Here is my code

class Program
{
static void Main(string[] args)
{
string buffer;
string[] map = new string[100];
int[] mapint = new int[100];
buffer = File.ReadAllText("D:/123.txt");
buffer = buffer.Replace("\r", string.Empty).Replace(" ", string.Empty);
map = buffer.Split(',');
for (int i = 0; i < 20; i++)
{
mapint = Convert.ToInt32(map);
Console.Write(mapint + " ");
}
Console.ReadKey();
}
}

so i want to do something like this


for(int x = 0; x < row; x++)
for(int y = 0; y < column; y++)
map[x, y] = mapint[x];


you know store the first row when y = 0; second row when y = 1; etc...

Share this post


Link to post
Share on other sites
Advertisement
Whichever variable you want to be a 2d array needs to be declared as such. I honestly don't remember the syntax; I never use arrays.

Share this post


Link to post
Share on other sites
When doing this, I store the Width and Height of the 2D map together with the 1D array, so I have everything I need when parsing it back.

Converting from 2D to 1D is easy, since you just concatenate all values. Now, when parsing back, you must know how many columns a line has. Unless all of your maps are of a fixed size, lets say, 10x10. You will have to store this data.

After that, it's easy:


Map = new int[Height, Width];
for (int i = 0; i < Width; i++)
for (int j = 0; j < Height; j++)
Map[j, i] = strArray[j * Width + i];



If you don't store this data and all of your values are of fixed size, its even easier, just replace the values on the above example for the fixed values.

example:

You have an array like this, which is composed of 100 items. All of your maps are 10x10, so you'll always have 100 items on the 1d array. To parse this back to 2D, all you gotta do is


1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,



Map = new int[10, 10];
for (int x = 0; x < 10; x++)
for (int i = 0; y < 10; y++)
Map[y, x] = strArray[y * 10 + x];
Edited by Arthur Souza

Share this post


Link to post
Share on other sites
I always hated when someone replied to me like this, so please take this as a suggestion and not a criticism!
I think what your doing may not be the best way to save and load your map data.

But to create a 2d array you do the following:


int[,] MapData = new int[100, 100];



I would suggest saving your map cordinates along with the tile info, without over complicating things you could just parse the first int as X the second int as Y and the 3rd int as the tile number.

Share this post


Link to post
Share on other sites
well I want mapint to be 2d array.

Here how i want it. for example the first 9 numbers in mapint[] to be copied to int Map[0, 0], Map[1, 0], Map[2, 0]. etc...

then from element 9, 10 , 11, to 20 int mapint[] to be copied to Map[0, 1], Map[1, 1], Map[2, 1] etc...

Share this post


Link to post
Share on other sites

I always hated when someone replied to me like this, so please take this as a suggestion and not a criticism!
I think what your doing may not be the best way to save and load your map data.
But to create a 2d array you do the following:

int[,] MapData = new int[100, 100];

I would suggest saving your map cordinates along with the tile info, without over complicating things you could just parse the first int as X the second int as Y and the 3rd int as the tile number.


I know how to create a 2D array. I know that i need to do this

int[,] MapData = new int[100, 100];


I know what you mean and I have tried your way long time ago and it was very messy and i didn't really like it. I thought my way was cleaner. I don't know. I Just prefer my way biggrin.png

thank you anyways.

When doing this, I store the Width and Height of the 2D map together with the 1D array, so I have everything I need when parsing it back.

Converting from 2D to 1D is easy, since you just concatenate all values. Now, when parsing back, you must know how many columns a line has. Unless all of your maps are of a fixed size, lets say, 10x10. You will have to store this data.

After that, it's easy:


Map = new int[Height, Width];
for (int i = 0; i < Width; i++)
for (int j = 0; j < Height; j++)
Map[j, i] = strArray[j * Width + i];



If you don't store this data and all of your values are of fixed size, its even easier, just replace the values on the above example for the fixed values.

example:

You have an array like this, which is composed of 100 items. All of your maps are 10x10, so you'll always have 100 items on the 1d array. To parse this back to 2D, all you gotta do is


1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,



Map = new int[10, 10];
for (int x = 0; x < 10; x++)
for (int i = 0; y < 10; y++)
Map[y, x] = strArray[y * 10 + x];



that looks like what i want. i'll give it a try. thx Edited by FantasyVII

Share this post


Link to post
Share on other sites



thank you very much it worked perfectly.

but can I ask you one more favor.

can you explain this line of code?


Map[y, x] = strArray[y * 10 + x];


I can't understand it. Why did you do [y * 10 + x] ?!!!


Here is the full code if anyone is interested.

File

1,2,3,4,5,6,7,8,9,
10,11,12,13,14,15,16,17,18,
19,20,21,22,23,24,25,26,27,
28,29,30,31,32,33,34,35,36,



class Program
{
static void Main(string[] args)
{
string buffer;


string[] map = new string[100];

int[] mapint = new int[100];

int[,] Map = new int[10, 10];

buffer = File.ReadAllText("D:/123.txt");


buffer = buffer.Replace("\r", string.Empty).Replace(" ", string.Empty);

map = buffer.Split(',');

for (int i = 0; i < 36; i++)
mapint = Convert.ToInt32(map);

for (int x = 0; x < 10; x++)
for (int y = 0; y < 10; y++)
Map[y, x] = mapint[y * 10 + x];

for (int x = 0; x < 10; x++)
{
Console.WriteLine();
for (int y = 0; y < 10; y++)
{
Console.Write(Map[x, y] + " ");
}
}
Console.ReadKey();
}
}
Edited by FantasyVII

Share this post


Link to post
Share on other sites
Sure thing!

Lets review the process of getting stuff back from the 1d array to the 2d array.

Each line has 10 items. So, for the first line [index 0] you just get the first 10 items. For the second line [index 1], you will get the second group of 10 items after the first 10 items on the 1d array.

Do you follow? This translates to, the items you want to get from the 1d array, start at LineIndex * ValuesPerLine, which means Y * 10.

Ok, so, the second line starts at LineIndex * ValuesPerLine, now to get the exact column you want, you just add its index. LineIndex * ValuesPerLine + ColumnIndex

After that, you add X, the current column index. Understood?

[X, X, X, X, X, X, X, X, X, X] Becomes [X, X, X, X, X, X, X, X, X, X][X, X, [color=#ff0000]X, X, X, X, X, X, X, X]
[X, X, [color=#ff0000]X, X, X, X, X, X, X, X] Edited by Arthur Souza

Share this post


Link to post
Share on other sites

Sure thing!

Lets review the process of getting stuff back from the 1d array to the 2d array.

Each line has 10 items. So, for the first line [index 0] you just get the first 10 items. For the second line [index 1], you will get the second group of 10 items after the first 10 items on the 1d array.

Do you follow? This translates to, the items you want to get from the 1d array, start at LineIndex * ValuesPerLine, which means Y * 10.

Ok, so, the second line starts at LineIndex * ValuesPerLine, now to get the exact column you want, you just add its index. LineIndex * ValuesPerLine + ColumnIndex

After that, you add X, the current column index. Understood?

[X, X, X, X, X, X, X, X, X, X] Becomes [X, X, X, X, X, X, X, X, X, X][X, X, [color=#ff0000]X, X, X, X, X, X, X, X]
[X, X, [color=#ff0000]X, X, X, X, X, X, X, X]


oooh ok now I get it.

thank you very much :D

I really appreciate it.

Share this post


Link to post
Share on other sites
...snip...


In fact, using this method removes the need to use 2D arrays. You can view a 2D array as being arranged in memory as as contiguous sequence of 1D arrays.

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!