- Home /
Count every row and column of multidemensional array (c# unity)
I have a multidimensional array which is declared like this
string[,] table = new string[104,15];
Now it has all the data that I need and I got the data to put it on the table by doing this
int xIndex = 0;
int yIndex = 0;
string newPreviousValue = "placeholder";
for (int i = 0; i < list.Count; i++)
{
newString[0] += list[i].r;
newString[0] += ",";
}
string[] newChars = newString[0].Split(',');
foreach (string previousValue in newChars)
{
table[xIndex, yIndex] = result;
}
Now what I am trying to do is
RED means "The same in length"
BLUE means "Not the same in length"
This Data A
has this kind of data now this Data B
depends on the value of Data A
has.
This Data B
came from Data A
so here it is how it works .
PSEUDO CODE
//I am talking about Data A
if table[1,1] is equal to null then
move to table[2,0] and compare table[1,0] and table[0,0]
//Display blue circle on Data B
display a blue circle
//I am talking about Data A
else if table[1,1] is not equal to null then
compare table[0,1] and table[0,0] if they have the same length as table[1,1] and table[1,0]
//Display a red circle
display a red circle
If I am not clear here's the rule . What I am talking about is the Big Eye Road Rules.
What I have tried so far is this
//lets check for the 2nd row and 2nd column of the big road table
if (table[1,1] == null && table[2,0] != null)
{
Move = true;
if (Move)
{
//lets move to the 3rd row and compare if they have the same in length (1st row and 1st column)
if (table[0, 0] != null && table[1, 0] != null)
{
//red circle
}
else
{
//blue circle
}
}
}
What I want to achieve here is to count every row and column in DATA A
if how many data's that are stored there for example
table[0,0] to table[0,6]
has 1 data
table[1,0] to table[1,6]
has 1 data
table[2,0] to table[2,6]
has 2 data
Just like that . Could someone help with it please. Thank you.
Answer by Scribe · May 22, 2018 at 10:24 AM
Well I basically didn't understand anything you wrote except the very last bit, so here's how you do the last bit!
for (int x = 0; x < table.GetLength(0); x++) {
int sum = 0;
for (int y = 0; y < table.GetLength(1); y++) {
if (table[x,y] != null) {
sum++;
}
}
Debug.Log("table column " + x + " has " + sum + " data");
}
While not directly wrong, this code does something similar as asked, it will go over the entire multidimensional array, and not between a range inside the array like for example from 1,0 to 1,6.
To achieve this effect you can make a method like so:
public static int AmountNotNullInRange<T>(T[,] array, int startX, int startY, int endX, int endY) where T : class
{
int sum = 0;
for (int x = startX; x < endX; x++)
{
for (int y = startY; y < endY; y++)
{
// Skip out of bounds indexes
if (startX > array.GetLength(0) - 1 || endX > array.GetLength(0) - 1) continue;
if (startY > array.GetLength(1) - 1 || endY > array.GetLength(1) - 1) continue;
// Check if the index is not null
if (array[x, y] != null)
sum++;
}
}
return sum;
}
And an example of me using it:
string[,] arr = new string[10,10];
System.Random rand = new System.Random();
for (int x = 0; x < 10; x++)
{
for (int y = 0; y < 10; y++)
{
arr[x, y] = rand.Next(3) == 2 ? "blabla" : null;
}
}
Debug.Log("Data available: " + AmountNotNullInRange(arr, 2, 2, 8, 8));
It is very helpful . I'll mark this as an answer though it wasn't fully answered my question . But thanks anyway :)
Your answer
Follow this Question
Related Questions
Trying to Sort a Multi Dimensional Array 1 Answer
Wierd IndexOutOfRangeException : Array index is out of range 1 Answer
C# Debug 3D Array 2 Answers
Are multidimensional arrays supported in UNET? 1 Answer
Multiple Cars not working 1 Answer