string to byte[] array - c#

ok i know this has been beat to death but i can't seam to get all the ones on google here or anyplace to work
i have a string that looks like this
1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
i store this string
each digit has a range from 0 to 254 and the string is almost random
i need to convert this to a byte array because of a hardware
change
this is what the hardware is looking for
hardware.command(DeviceID, byte[]);
i tested the hardware by doing this
hardware.command(1, new byte[] { 0, 0, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38 });
and all was good
each part is a PWM output percentage on the hardware
this updates every 4ms so it has to be somewhat fast

I don't know if this satisfies your performance requirements, but the easiest way would be to use LINQ:
var myByteArray = (from s in myString.Split(',') select byte.Parse(s)).ToArray();
EDIT: I originally wrote byte.Parse(s.Trim()), but Byte.Parse does not mind surrounding whitespace.

Try something like this:
string datastring = "1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0";
byte[] data = datastring.Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(s => byte.Parse(s)).ToArray();
or, without LINQ:
var datastring = "1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0";
var tokens = datastring.Split(',');
byte[] data = new byte[tokens.Length];
for (int i = 0; i < tokens.Length; i++) {
data[i] = byte.Parse(tokens[i]);
}

String[] substringArray = String.Split(',');
List<Bytes> listOfBytes = new List<Bytes>();
foreach(String substring in substringArray )
{
substring = substring.Trim();
listOfBytes.Add(Byte.Parse(substring));
}
Byte [] byteTable = listOfBytes.ToArray();

Related

C# Static Class cannot be found

I created a static class to keep all my "level" arrays in (I'm writing a game in monogame, using XNA).
The problem is that i can't use the class anywhere, it just won't show up in intellisense, and returns an error The name 'Rooms' does not exist in the current context. Any idea what may cause this?
namespace Marked_V0._2
{
public static class Rooms
{
static public int[,] Level1 = new int[,]
{
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1 }
};
}
}
Error:
CS1002 ; Expected
CS1001 Identifier expected
CS0103 The name 'Rooms' does not exist in the current context.
The namespace for Rooms most likely does not match the namespace of the rest of the project. You can either change the namespace or include the extra namespace with using Marked_V0._2.

Misreading a multidimensional array

I am trying to have a loop read a multidimensional array defined in a separate class to place objects at the start of a game.
This is the Start function code from the LevelStart class:
void Start () {
//Get Level
Levels levels = ScriptableObject.CreateInstance<Levels>();
level = levels.GetLevel (levelName);
x = level.GetLength(1);
y = level.GetLength (2);
temp = GameObject.Find ("temp").GetComponent<Temp>();
lPos = temp.GetLevelNum() - 1; //Chosen Level
//Read array and place objects
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
if (level[lPos,i,j] == 1) {
Debug.Log ("Spawn Point");
}
else if (level[lPos,i,j] == 2) {
Debug.Log ("Hole");
}
else if (level[lPos,i,j] == 3) {
Debug.Log ("Obstacle");
}
else
Debug.Log ("Open Space");
}
}
}
And the class holding the arrays is Levels.cs:
public int[,,] cave = new int[,,] {
{ //Level 1
{ 0, 0, 0, 3, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0},
{ 3, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 3, 0, 0},
{ 0, 3, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 3, 0, 0, 0, 0, 3, 0, 3, 0, 0, 0},
{ 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 3, 0},
{ 0, 0, 3, 0, 3, 0, 0, 3, 0, 0, 0, 0, 0, 0},
{ 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0},
{ 0, 0, 3, 0, 3, 0, 3, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 2, 0, 0, 0, 3, 0, 3, 0, 0, 0}
},
{ //Level 2
{ 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0 },
{ 0, 0, 0, 0, 3, 0, 0, 0, 3, 0, 0, 0, 0, 0 },
{ 0, 2, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 3, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 3, 0, 0 },
{ 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 3, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0 },
{ 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0 }
}
};
It reads most of the array correctly but positions such as [0,0,0] and [0,0,12] returns 3's when it should be 0
Thanks in advance
Seems to me to be pretty likely that you're simply using the wrong level number - code that is not shown. I suggest using the print statement to ensure temp.GetLevelNum() and lPos are the numbers you think they should be.

C# Convert a string to multidimensional int array

I made a tile level system that uses a multidimensinal array, for example:
new int[,]{
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0,},
{0, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
{0, 0, 0, 0, 2, 0, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0,},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,},
Every specific number represents a block(tile).
This is just in code. I want to code a system that can load a level(an multidimensinal int array)
That array needs to be converted from a string. How do I do that?
public static int[,] getLvl(string lvlName = "")
{
string readed = "";
using (StreamReader read = new StreamReader(path))
{
readed = read.ReadToEnd();
}
return null; //What to put here?!?!?!
}
EDIT: I do not have yet a format for the file to read. So you can be flexible in that.
I don't think you really need to or should bother trying to serialize it into XML or some other format, since your data storage is so simple.
One easy way is to just store your array in a text file as comma separated values. So one level might have:
0,0,0,1
0,1,1,0
0,1,1,3
3,3,4,1
The String.Split() method is really useful for parsing something simple like this. It allows you to split a string into an array of substrings based on a certain delimiting character.
Step by step:
First you can use var RowArray = MyString.Split('\n') (the newline character) to split your string into an array of rows. This leaves you with the array:
[0]: "0,0,0,1"
[1]: "0,1,1,0"
[2]: "0,1,1,3"
[3]: "3,3,4,1"
You can sort of see what Split() does here and why that's useful for your case. You can in turn run split each row on ',' leaving with you an array of arrays, which you can very easily convert to exactly the 2D array you're looking for.
The one pitfall here is somewhere, depending on your design needs, one invariant might have to be that in the file all rows will be of the same length. Or if you can't guarantee that, you'll have to write some code so that when turning the below text into an array from an array of rows, you make the width equal to the longest row and fill in blanks with 0s or some other method.
0,0,0,1,6,4
0,1,1,0
0,1,1,3,2,6,3,7,1
3,3,4,1,2,4
The shortest method is using linq, for this format:
0,0,0,0,0
1,0,1,0,1
....
You can use this sentence:
int[][] Data = File.ReadAllLines( path )
.Select( s => s.Trim())
.Where( s => !string.IsNullOrempty(s))
.Select( s => s.Split( ',' )
.Select( token => int.Parse( token ) )
.ToArray( ) )
.ToArray( );
var DataToOneDim = Data.SelectMany( a => a).ToArray();
var Result = new int[Data[0].Length, Data.Length];
Buffer.BlockCopy( DataToOneDim, 0, Result, 0, DataToOneDim.Length );
I believe there are a few other posts regarding this, but you can use either XML and parse through each possible dimension or serialize everything at once using the SoapFormatter class. Below is a link to a similar question with some examples:
Convert Multi-Dimensional Array to String and Back
I make it for you fast you can make it better do not Forget the vote :_)
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
internal class Program
{
private static void Main(string[] args)
{
var array =
"{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,} , {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,}";
string input = array;
string pattern = #"{|}";
var index = 0;
var results = new int[100,100];
foreach (var result in Regex.Split(input, pattern))
{
var sp = result.Split(',');
if (sp.Length <4)
continue;
for (int i = 0; i < sp.Count(); i++)
{
if (!string.IsNullOrEmpty(sp[i]))
results[index,i] = Convert.ToInt32(sp[i]);
}
index++;
}
}
}
}

How use marshal.Copy function to read data into memory?

I'm trying to port a desencrypt function i have in Delphi to C#.
in delphi i use the function Move to get data into memory using a pointer, so i tried to use the marshal.copy function with the same results but sometimes i get an acces violation exception and sometimes blank spaces.
leave attached the code of the function.
Any ideas? Thanks a lot!
public string prDecode(string s)
{
byte[] ArrayMap = new byte[256] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 62, 0, 0, 0, 63, 52, 53, 54, 55, 56, 57, 58, 59,60,
61, 0, 0, 0, 0, 0, 0, 0, 0, 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,
0, 0, 0, 0, 0, 0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
51, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int I;
char[] ArrayS = s.ToCharArray(0, s.Length);
string resultado = "";
char[] ArrayR = resultado.ToCharArray(0, resultado.Length);
switch (s.Length)
{
case 2:
I = ArrayMap[ArrayS[0]] + (ArrayMap[ArrayS[1]] << 6);
ArrayR = new char[1];
Marshal.Copy((IntPtr)I, ArrayR, 0, ArrayR.Length);
break;
case 3:
I = ArrayMap[ArrayS[0]] + (ArrayMap[ArrayS[1]] << 6) +
(ArrayMap[ArrayS[2]] << 12);
ArrayR = new char[2];
Marshal.Copy((IntPtr)I, ArrayR, 0, ArrayR.Length);
break;
case 4:
I = ArrayMap[ArrayS[0]] + (ArrayMap[ArrayS[1]] << 6) +
(ArrayMap[ArrayS[2]] << 12) + (ArrayMap[ArrayS[3]] << 18);
ArrayR = new char[3];
Marshal.Copy((IntPtr)I, ArrayR, 0, ArrayR.Length);
break;
}
string clave = new string (ArrayR);
return clave;
}
I think the function you're looking for is Array.Copy or Buffer.BlockCopy rather than Marshal.Copy. Marshal.Copy is for copying data to and from unmanaged memory.
There's no need to use Marshal unless you are accessing unmanaged memory for legacy code. Managed code has no need for pointers & using managed methods will be much quicker.
What you're looking for I think, is Array.Copy to copy between arrays using managed code.

C# declaring a 2D array

I am trying to setup a 2d array in C# to act as a maze to move a character around, I am having a few issues initialising the array, I am trying to do the below
but the InitialiseMaze method is saying the maze is not declared
Can anyone advise
thanks
simon
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GameMan
{
public class Maze
{
#region Variables
static int[,] maze;
#endregion
#region Constructors/Destructors
public Maze()
{
InitaliseMaze();
}
~Maze()
{
}
#endregion
#region Methods
public void InitaliseMaze()
{
maze = {
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 0, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0},
{0, 0, 3, 0, 0, 2, 0, 0, 0, 2, 0, 2, 0, 0, 0, 2, 0, 0, 3, 0, 0},
{0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0},
{0, 0, 2, 0, 0, 2, 0, 2, 0, 0, 0, 0, 0, 2, 0, 2, 0, 0, 2, 0, 0},
{0, 0, 2, 2, 2, 2, 0, 2, 2, 2, 0, 2, 2, 2, 0, 2, 2, 2, 2, 0, 0},
{0, 0, 0, 0, 0, 2, 0, 0, 0, 1, 0, 1, 0, 0, 0, 2, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 2, 0, 1, 1, 1, 4, 1, 1, 1, 0, 2, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 2, 0, 1, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0},
{1, 1, 1, 1, 1, 2, 1, 1, 0, 0, 0, 0, 0, 1, 1, 2, 1, 1, 1, 1, 1},
{0, 0, 0, 0, 0, 2, 0, 1, 0, 0, 0, 0, 0, 1, 1, 2, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 2, 0, 1, 1, 1, 1, 1, 1, 1, 0, 2, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 2, 0, 1, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0},
{0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 0, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0},
{0, 0, 2, 0, 0, 2, 0, 0, 0, 2, 0, 2, 0, 0, 0, 2, 0 ,0, 2, 0, 0},
{0, 0, 3, 2, 0, 2, 2, 2, 2, 2, 5, 2, 2, 2, 2, 2, 0, 2, 3, 0, 0},
{0, 0, 0, 2, 0, 2, 0, 2, 0, 0, 0, 0, 0, 2, 0, 2, 0, 2, 0, 0, 0},
{0, 0, 2, 2, 2, 2, 0, 2, 2, 2, 0, 2, 2, 2, 0, 2, 2, 2, 2, 0, 0},
{0, 0, 2, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 0, 0, 0, 0, 0, 2, 0, 0},
{0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
};
}
#endregion
}
}
You can't initialize an array like that other than in a variable declaration. However, the change is simple:
maze = new int[,] {
// As before
};
As asides:
It looks like maze should be an instance variable rather than a static variable. After all, you're initializing it each time you create an instance of Maze
You have a finalizer for no reason. Finalizers are very rarely required (or indeed advisable) in C#
Ok, well here is some extract from the msdn :
int[,] myArray = {{1,2}, {3,4}, {5,6}, {7,8}};
extracted from MSDN multidimensional arrays
you should also read up concerning Destructors, finalizers etc ... , I bet your coming from C++ ? Differences between the 2 languages arent always obvious :).
Just to make Jon's post a bit clearer:
maze = new int[,]{
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 0, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0},
{0, 0, 3, 0, 0, 2, 0, 0, 0, 2, 0, 2, 0, 0, 0, 2, 0, 0, 3, 0, 0},
{0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0},
{0, 0, 2, 0, 0, 2, 0, 2, 0, 0, 0, 0, 0, 2, 0, 2, 0, 0, 2, 0, 0},
{0, 0, 2, 2, 2, 2, 0, 2, 2, 2, 0, 2, 2, 2, 0, 2, 2, 2, 2, 0, 0},
{0, 0, 0, 0, 0, 2, 0, 0, 0, 1, 0, 1, 0, 0, 0, 2, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 2, 0, 1, 1, 1, 4, 1, 1, 1, 0, 2, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 2, 0, 1, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0},
{1, 1, 1, 1, 1, 2, 1, 1, 0, 0, 0, 0, 0, 1, 1, 2, 1, 1, 1, 1, 1},
{0, 0, 0, 0, 0, 2, 0, 1, 0, 0, 0, 0, 0, 1, 1, 2, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 2, 0, 1, 1, 1, 1, 1, 1, 1, 0, 2, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 2, 0, 1, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0},
{0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 0, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0},
{0, 0, 2, 0, 0, 2, 0, 0, 0, 2, 0, 2, 0, 0, 0, 2, 0 ,0, 2, 0, 0},
{0, 0, 3, 2, 0, 2, 2, 2, 2, 2, 5, 2, 2, 2, 2, 2, 0, 2, 3, 0, 0},
{0, 0, 0, 2, 0, 2, 0, 2, 0, 0, 0, 0, 0, 2, 0, 2, 0, 2, 0, 0, 0},
{0, 0, 2, 2, 2, 2, 0, 2, 2, 2, 0, 2, 2, 2, 0, 2, 2, 2, 2, 0, 0},
{0, 0, 2, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 0, 0, 0, 0, 0, 2, 0, 0},
{0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
};
Boy, that was a big maze array...
You need to declare maze
numbers = new int[X,Y]; where X and Y are how big it is
Two-Dimensional Arrays
The simplest form of the multidimensional array is the 2-dimensional array. A 2-dimensional array is a list of one-dimensional arrays.
A 2-dimensional array can be thought of as a table, which has x number of rows and y number of columns. Following is a 2-dimensional array, which contains 3 rows and 4 columns −
Two Dimensional Arrays in C#
Thus, every element in the array a is identified by an element name of the form a[ i , j ], where a is the name of the array, and i and j are the subscripts that uniquely identify each element in array a.
Initializing Two-Dimensional Arrays
int [,] a = new int [3,4] {
{0, 1, 2, 3} , /* initializers for row indexed by 0 */
{4, 5, 6, 7} , /* initializers for row indexed by 1 */
{8, 9, 10, 11} /* initializers for row indexed by 2 */
};
Explain above code:
new int [**3**,4] **3** denoting to rows like how may object in array
eg:
{0, 1, 2, 3} ,
{4, 5, 6, 7} ,
{8, 9, 10, 11}
new int [3,**4**] **4** denoting to columns like total value in object (4 columns)
eg:
{0, 1, 2, 3}
Let us check the program to handle a two dimensional array
using System;
namespace ArrayApplication {
class MyArray {
static void Main(string[] args) {
/* an array with 5 rows and 2 columns*/
int[,] a = new int[5, 2] {{0,0}, {1,2}, {2,4}, {3,6}, {4,8} };
int i, j;
/* output each array element's value */
for (i = 0; i < 5; i++) {
for (j = 0; j < 2; j++) {
Console.WriteLine("a[{0},{1}] = {2}", i, j, a[i,j]);
}
}
Console.ReadKey();
}
}
}
Output:
a[0,0]: 0
a[0,1]: 0
a[1,0]: 1
a[1,1]: 2
a[2,0]: 2
a[2,1]: 4
a[3,0]: 3
a[3,1]: 6
a[4,0]: 4
a[4,1]: 8

Categories

Resources