c# array multidimensional or jagged -
i'm drawing 3x3 grid of squares 2d game map.
i want create array has row , column position , each row , column position, has 4 0's or 1's representing if wall drawn on edge of square.
i want create array:
int [,][,,,] boxes = {{0,0}, {1,0,0,1}, {1,0}, {1,0,1,0}, {0,1}, {0,0,0,1}, {1,1}, {1,1,1,0}, {2,0}, {1,1,0,0}, {2,1}, {0,0,0,1}, {3,1}, {1,0,1,0}, {0,2}, {0,0,1,1}, {1,2}, {1,0,1,0}, {2,2}, {0,1,1,0}};
however, seems not correct.
i have tried this:
int [][] boxes = new int [2][4] boxes = {{0,0}, {1,0,0,1}, {1,0}, {1,0,1,0}, {0,1}, {0,0,0,1}, {1,1}, {1,1,1,0}, {2,0}, {1,1,0,0}, {2,1}, {0,0,0,1}, {3,1}, {1,0,1,0}, {0,2}, {0,0,1,1}, {1,2}, {1,0,1,0}, {2,2}, {0,1,1,0}};
is clear, type of array trying make?
how this?
thanks
a problem first approach int[,][,,,] 2 dimensional array of 4 dimensional arrays. [,] refers array of form {{0, 0}, {0, 0}}, no of form {0, 0}. commas indicate number of dimensions, not number of elements.
your second approach closer. creating array of correct dimensions, can't initialize arrays within array inline that. more this:
int [][] boxes = new int[][] { new int[] {1,0}, new int[] {1,0,1,0}, new int[] {0,1}, new int[] {0,0,0,1}, new int[] {1,1}, new int[] {1,1,1,0}, new int[] {2,0}, new int[] {1,1,0,0}, new int[] {2,1}, new int[] {0,0,0,1}, new int[] {3,1}, new int[] {1,0,1,0}, new int[] {0,2}, new int[] {0,0,1,1}, new int[] {1,2}, new int[] {1,0,1,0}, new int[] {2,2}, new int[] {0,1,1,0}};
which is, quite frankly, hideous.
one thing stands out here wrote new int [2][4]
. [2][4] doesn't refer array of size 2 followed array of size 4. refers array of size 2 of arrays of size 4, , important distinction. looks difference between dimensions , length, appears main problem you're having in getting code work.
as others have mentioned, design isn't making use of tools have. code if used objects encapsulate data, instead of putting multiple types of data in same array. gets confusing , error prone.
Comments
Post a Comment