Simple Q about multidimension array .. plz Help
-
how can i get index of specified item in multidimension array ?
-
how can i get index of specified item in multidimension array ?
By using nested for loops to step through and checking for when the element is equal to your value.
valueSearch = "Value to search for";
for (int x = 0; x < array.GetLength(0); x++)
{
for (int y = 0; y < array.GetLength(1); y++)
{
for (int z = 0; z < array.GetLength(2); z++)
{
if (array[x][y][z] == valueSearch)
{
//store values for x, y and z. Those will be your indices
}
}
}
}There are 10 types of people in the world, those who understand binary and those who dont.
-
By using nested for loops to step through and checking for when the element is equal to your value.
valueSearch = "Value to search for";
for (int x = 0; x < array.GetLength(0); x++)
{
for (int y = 0; y < array.GetLength(1); y++)
{
for (int z = 0; z < array.GetLength(2); z++)
{
if (array[x][y][z] == valueSearch)
{
//store values for x, y and z. Those will be your indices
}
}
}
}There are 10 types of people in the world, those who understand binary and those who dont.
I would say that if you need to be searching, arrays aren't the way to go. But if you must use arrays, the only tip I would add to the above is a break statement jump out of the nested for loops when you actually find the value (no need in running through the rest of the items).
J
Make the logo bigger
-
I would say that if you need to be searching, arrays aren't the way to go. But if you must use arrays, the only tip I would add to the above is a break statement jump out of the nested for loops when you actually find the value (no need in running through the rest of the items).
J
Make the logo bigger