changing this to 1D array
-
10 aprilie 2012 07:58
//Wall array
//Vertical (16x16)
int[][] a = new int[][] {new int[] { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
new int[] { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
new int[] { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
new int[] { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
new int[] { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
new int[] { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
new int[] { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
new int[] { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
new int[] { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
new int[] { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
new int[] { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
new int[] { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
new int[] { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
new int[] { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
new int[] { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
new int[] { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
//Horizontal (16x16)
int[][] b = new int[][]{ new int[] { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
new int[] { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
new int[] { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
new int[] { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
new int[] { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
new int[] { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
new int[] { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
new int[] { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
new int[] { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
new int[] { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
new int[] { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
new int[] { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
new int[] { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
new int[] { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
new int[] { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
new int[] { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};int x = 0;
int y = 0;.
.
northWall[15][0] = a[15][0]; // x=0,y=0
eastWall[14][0] = b[14][0];
// pass to solver
// solver(...)
SearchToGoal(northWall1, eastWall1);
// solver return mouse command
//mouse move .. update x ,y
northWall[15 - y][0] = a[15 - y][0]; // x=0,y=1
eastWall[14 - y][0] = b[14 - y][0];
// pass to solver
northWall[15 - y][x] = a[15 - y][x]; // x=1,y=1
eastWall[14 - y][x] = b[14 - y][x];- Editat de Melsoon 10 aprilie 2012 08:17
Toate mesajele
-
10 aprilie 2012 09:53
Hi Melsoon,
Do you want to only change the arrays a and b to 1D array or change all arrays to 1D array?
If you only request the former, you can write array a and b to 1D in sequence. And acquire northWall or eastWall as below:
northWall[15][0] = a[15*16+0];
eastWall[15][0] = b[15*16+0];
Sucie Shu
Version Control | SCM | TWAIN
- Marcat ca răspuns de Melsoon 12 aprilie 2012 05:54
-
10 aprilie 2012 14:38
To make 'a' a 1D array you can do this:
int[] array = a.SelectMany(item => item).ToArray();
- Marcat ca răspuns de Melsoon 12 aprilie 2012 05:54
-
10 aprilie 2012 19:11
int m_rows = 16; int m_cols = 16; int[] table = new int[m_rows * m_cols](); private void BuildTable() { for(int i = 0; i < m_rows * m_cols; i++) { table[i] = 0; } } public int GetValueAt(int pX. int pY) { int cpt = 0; //lets see how many cels we passed on previous rows if(pY > 0) cpt = pY * m_cols; //we must remove 1 because we work with the index return table[cpt + pX -1]; }^^I guess you could do sumthing like this...
- Editat de Abyte Zero 10 aprilie 2012 19:15
- Propus ca răspuns de Abyte Zero 10 aprilie 2012 19:16
-
10 aprilie 2012 19:40@Abyte Zero, it's considered bad form to propose your own post as the answer, in general. The fact that you posted it obviously means you think it's the answer. The mechanism is there so that other people can 'agree' with you and not spam the thread with 'do what he did' posts.
-
16 aprilie 2012 02:48
Hi guys I'm sorry but can you all give me a specific answer for at least 'a'?
How to change this to 1D? At least give me a specific answer for this and i can do 'b' myself.
I'm not good with programming so please help out. I appreciated it very much!
int[][] a = new int[][] {new int[] { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
new int[] { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
new int[] { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
new int[] { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
new int[] { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
new int[] { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
new int[] { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
new int[] { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
new int[] { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
new int[] { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
new int[] { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
new int[] { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
new int[] { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
new int[] { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
new int[] { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
new int[] { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}; -
16 aprilie 2012 11:10
I wrote a function for you and tried to comment it as good as I could:
public int[] Int2DTo1D(int[][] Int2DArray) { if(Int2DArray != null) { // Variable for containing the total amount of integer values found within the array. int TotalArrayLength = 0; for (int i = 0; i < Int2DArray.Length; i++) { // Loop through all sub arrays to get the total amount of integer values. if (Int2DArray[i] != null) { // Prevent the runtime error when calling the length of a null array. // Add the array length to the array length counter. TotalArrayLength += Int2DArray[i].Length; } } if (TotalArrayLength == 0) { // There is no values within the array. return null; } // Create a 1d array buffer to put all the arrays into. int[] Int1DArray = new int[TotalArrayLength]; // Value for storing our current array position. int Position = 0; for (int i = 0; i < Int2DArray.Length; i++) { // Loop through all sub arrays again to add them to the 1d array buffer. if (Int2DArray[i] != null) { // Prevent the runtime error when calling the length of a null array. for (int x = 0; x < Int2DArray[i].Length; x++) { // Inserting the value into the 1d array buffer. Int1DArray[Position] = Int2DArray[i][x]; // Updating the position to insert the next int value. Position++; } } } // Now we are done and ready to return the 1d array. return Int1DArray; } else { // Array is empty. return null; } }
Thanks
Karl Oskar
- Editat de Karl Oskar Andersen 16 aprilie 2012 11:22 Minor bug fix
-
16 aprilie 2012 13:52
Hi guys I'm sorry but can you all give me a specific answer for at least 'a'?
How to change this to 1D? At least give me a specific answer for this and i can do 'b' myself.
Do you want to change it programmatically at runtime, or at compile time so that the two dimensional array will no longer exist in the program at all?
I gave you a single line of code that does the former, and Abyte Zero gave you a segment of code that does the latter.