I'm trying to declare a 3 dimensional array in C#, and I'm having some errors thrown at me. This line is the culprit: Code (csharp): public int[] 3dArray = new int[ , , ]; this gives me the error "new expression required () or [] after type. So if I change it to: Code (csharp): public int[] 3dArray = new int[3]; and I get the error "incorrect number of indexes for array. expected 1 got 3" What's the correct way to declare multidimensional arrays in C# (and JS while I'm at it)?
int[,] myArray = new int[4,2]; http://msdn.microsoft.com/library/d...us/csref/html/vclrfmultidimensionalarrays.asp
thanks Joachim, it works if I explicitly define my array's dimensions, but what if I want my array to have a dynamic size based on the variables in the script? When working in C# in windows apps, the declaration Code (csharp): int[ , , ] myArray = new int[] works just fine, but Unity yells at me. Am I forced to declare my array's dimensions when I instantiate it?
You can of course use ArrayList and embed them inside each other. Not sure why your example compiles on windows, but as far as i know the C# specification requires you to declare the array dimension. Of course csc could have more relaxed behaviour than in the spec or i might be wrong and mono's C# compiler has a bug.
On C# you can do this for a 3 dimensional array: Code (csharp): int[,,] myArray = new int[,,]{} the part of the {} is the initializer, giving the initial values, and if you have to set the values it could be something like this: Code (csharp): int[ , , ] myArray = new int[]{{{1,2},{3,4},{5,6}},{{7,8},{9,10},{11,12}},{{13,14},{15,16},{17,18}} This type of arrays are fixed arrays, you would have to have arrays defined as items on other arrays: Code (csharp): ArrayList firstDimension = new ArrayList(); ArrayList secondDimensionChild = new ArrayList(); ArrayList thirdDimensionChild = new ArrayList(); firstDimension.Add(secondDimensionChild); secondDimensionChild.Add(thirdDimensionChild); thirdDimensionChild.Add(3); Then yo can access them through the ArraList's indexer (indexing 0 based): Code (csharp): int value = (int)firstDimension[0][0][0];
I'm not sure if this is the most efficient way or if this has some artifacts that I haven't noticed, but what I found is that Unity doesn't mind declaring multiple dimension arrays Code (CSharp): void initDungeonPoints(){ voxelPoints = new float[xCount][][];//sets up the array with an x size of [xCount] // loops through the 3D array and sets the value for the first dimension to // a 2d array with the "x" dimension that is acting as the 3D array's // y set to the y size of [ySize] for (int x = xCount-1; x>=0; x--){ voxelPoints[x] = new float[yCount][]; // loops through the 2D array and sets the value of the first dimension of the // 2D array to a 1D array of size [zCount] for (int y = yCount-1; y>=0; y--){ voxelPoints[x][y] = new float[zCount]; //loops through the final array and sets all of the points to a float value of 1 for (int z = zCount-1; z>=0; z--){ voxelPoints[x][y][z] = 1f; } } } } This might not work in previous versions of Unity, but it seems stable enough for my purposes.
What you have there is not a multi-dimensional array. It is a one-dimensional array of one-dimensional arrays of one-dimensional arrays. You could still use it to store the same data, but It's not exactly the same thing.