Click here to Skip to main content
15,916,288 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do I add more bounds the following array?

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleOne
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a 4x2 array (a grid with four rows and two columns).
            int[,] intArray = { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
            int rows = intArray.GetUpperBound(0); // rows = 4
            int columns = intArray.GetUpperBound(1); // columns = 2
            int third = intArray.GetUpperBound(2); //3rd dimension
            Console.WriteLine("rows= " + rows + " columns= " + columns);
            Console.WriteLine("third= " + third);
            Console.ReadKey(true);
        }
    }
}


Multidimensional arrays showing how to create 2d arrays here.
Posted

C#
using System;

public class CSharpApp 
{
    static void Main() 
    {

        int[,,] n3 = {
            {{12, 2, 8}},
            {{14, 5, 2}}, 
            {{3, 26, 9}},
            {{4, 11, 2}}
        };

        int d1 = n3.GetLength(0);
        int d2 = n3.GetLength(1);
        int d3 = n3.GetLength(2);

        for (int i=0; i<d1;>
        {
            for (int j=0; j<d2;>
            {
                for (int k=0; k<d3;>
                {
                    Console.Write(n3[i, j, k] + " ");
                } 
            } 
        }       
       Console.Write('\n');
   }
}
 
Share this answer
 
v2
Three dimensional arrays are very similar to two dimensional.

C#
int[, ,] Array3d = new int[2, 2, 3] { { { 1, 2, 3 }, { 4, 5, 6 } },
                                       { { 7, 8, 9 }, { 10, 11, 12 } } };

            Debug.WriteLine(Array3d[0, 0, 0]);
            Debug.WriteLine(Array3d[0, 0, 1]);
            Debug.WriteLine(Array3d[0, 0, 2]);

            Debug.WriteLine(Array3d[0, 1, 0]);
            Debug.WriteLine(Array3d[0, 1, 1]);
            Debug.WriteLine(Array3d[0, 1, 2]);

            Debug.WriteLine(Array3d[1, 0, 0]);
            Debug.WriteLine(Array3d[1, 0, 1]);
            Debug.WriteLine(Array3d[1, 0, 2]);

            Debug.WriteLine(Array3d[1, 1, 0]);
            Debug.WriteLine(Array3d[1, 1, 1]);
            Debug.WriteLine(Array3d[1, 1, 2]);


Try the above code.

This is very basic way of implementing three-d arrays. for more complex purposes, different data structures can be used to implement three-d arrays.

Hope this is useful.
 
Share this answer
 
Hi,


A link is sufficient to understand 3D Array, and here it is : Multidimentional Array[^]

Thanks
-Amit Gajjar
 
Share this answer
 
C#
class Sample
    {
        public static void Main(string[] args)
        {
            int[,,] intArray =
                {
                    {
                        {1,2,3}, {4,5,6}, {7,8,9}
                    },
                    {
                        {10,11,12}, {13,14,15}, {16,17,18}
                    }
                };
        }
    }

..and so on...
 
Share this answer
 
Comments
Kenneth Haugland 3-Sep-12 18:50pm    
5'ed :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900