For my C# project, I'm working with a 16x16x16 3D char array(char[,,]) and need to set the values of all elements to char '0'. Preferably during definition (char[,,] my3Dbasis = ...). Of course, I could do this manually, but such would be extremely unconventional and not suitable for my needs. Any ideas?
-
Does this answer your question? How to assign value in two dimensional array and C#: Setting all values in an array with arbitrary dimensionsuser12031933– user120319332021-06-20 18:25:08 +00:00Commented Jun 20, 2021 at 18:25
-
Also see stackoverflow.com/questions/15079215/…Matthew Watson– Matthew Watson2021-06-20 19:54:05 +00:00Commented Jun 20, 2021 at 19:54
Add a comment
|
2 Answers
You can do this something like this during initialization
char[,,] array3D = new char[2, 2, 3]
{
{
{ '0', '0', '0' },
{ '0', '0', '0' }
},
{
{ '0', '0', '0' },
{ '0', '0', '0' }
}
};
Or just iterate over the array and set the value
for ( int i = 0; i < 2; i++ )
{
for ( int j = 0; j < 2; j++ )
{
for ( int k = 0; k < 3; k++ )
{
array3D[i, j, k] = '0';
}
}
}
3 Comments
Array.GetUpperBound is preferable to literals. Also Array.GetLowerBound to be very scrupulous.
Matthew Watson
I assume you meant to use a
char array and to assign '0' rather than 0 (since assigning 0 like that is pointless because the array will already be initialised to contain all 0 values).Amit Kotha
Yes just changed to char array. Thanks for pointing it out