- Home /
How to remove null's from 2d array ? It's removing but only 2 out of 4 null's.
public Transform[,] clear_array_nulls(Transform[,] input)
{
int m = input.GetUpperBound(0);
int n = input.GetUpperBound(1) + 1;
Transform[] temp = new Transform[input.GetUpperBound(0)];
for (int x = 0; x < m; x++)
temp[x] = input[x, 0];
temp = temp.Where(s => !Transform.Equals(s, null)).ToArray();
Transform[,] output = new Transform[temp.Length, n];
Array.Copy(input, output, temp.Length * n);
return output;
}
Using it like this :
gridBlocks = clear_array_nulls(gridBlocks);
If for example the grid 2d array is in this case 10 on 10 than it will remove 2 null's from the inner array but there are two also in the outer array.
If I didn't mix between the inner and outer but close to the end of the 2d array it's removing 2 null's and more in the start still two null's left that I'm not sure how to remove them too.
One null is in at 0,0 and another null at 0,9 anyway why it's not removing all the null's ?
What do you mean by outer and inner arrays? Logically there are 10 arrays of size 10 in 10x10 array whose values you can reference by two indexes (think about like rows and columns). So there is no inner or outer array of values, only indexes. If you want to maintain the array's two dimensions then you can only remove whole rows or columns and you cannot make any "holes" to it.
If you really want to get rid of the records that are nulls then the best way is to use List(s) or Dictionary instead of 2D array. You can also convert the 2D array to 1D and make the deletion OR maintain the whole 2D array and just replace the null values with some other reference (that you decide to represent null or "removed" record)
Your answer
Follow this Question
Related Questions
Array Within Index 1 Answer
2d Array persists during multiple game sessions 2 Answers
2D array generated with custom inspector, null when starting game? 1 Answer
2D Array of GameObjects... 1 Answer
Multidimensional array trouble 2 Answers