Looping Through An Array C#

C# Code Snippets 1D 2D 3D Array Looping
Share:

About

In this code snippet, we’ll find out how to loop through arrays in C#.

The following C# code snippet contains the code for looping through 1D, 2D, and 3D arrays, filling them up with random numbers and then writing out the values from the arrays.

Code:

using System;

namespace ArrayLooping
{
    class Program
    {
        static void Main(string[] args)
        {
            //Make empty array.(Each "dimension" of a multidimnensional array can have a different size.)
            int[] oneDimensionalArray = new int[10];
            int[,] twoDimensionalArray = new int[10,5];
            int[,,] threeDimensionalArray = new int[10,5,15];

            //Fill the arrays with random numbers.
            oneDimensionalArray = fill1DArray(oneDimensionalArray);
            twoDimensionalArray = fill2DArray(twoDimensionalArray);
            threeDimensionalArray = fill3DArray(threeDimensionalArray);

            //Write out the contents of the arrays.
            Console.WriteLine("1D Array");
            print1DArray(oneDimensionalArray);
            Console.WriteLine("");

            Console.WriteLine("2D Array");
            print2DArray(twoDimensionalArray);
            Console.WriteLine("");

            Console.WriteLine("3D Array:");
            print3DArray(threeDimensionalArray);

            Console.ReadLine();
        }

        public static void print1DArray(int[] array)
        {
            for (int i = 0; i < array.Length; i++)
            {
                Console.Write(array[i]+", ");
            }
        }

        public static void print2DArray(int[,] array)
        {
            //Loop through array. (We use array.GetLength(0) with the index of the "dimension" to get the size.)
            for (int i = 0; i < array.GetLength(0); i++)
            {
                for (int j = 0; j < array.GetLength(1); j++)
                {
                    Console.Write(array[i,j] + ", ");
                }
            }
        }

        public static void print3DArray(int[,,] array)
        {
            //Loop through array. (We use array.GetLength(0) with the index of the "dimension" to get the size.)
            for (int i = 0; i < array.GetLength(0); i++)
            {
                for (int j = 0; j < array.GetLength(1); j++)
                {
                    for (int k = 0; k < array.GetLength(2); k++)
                    {
                        Console.Write(array[i,j,k] + ", ");
                    }
                }
            }
        }

        public static int[] fill1DArray(int[] array)
        {
            Random rnd = new Random();

            //Loop through array.
            for (int i = 0; i < array.Length; i++)
            {   
                //Save a random number form 1-10 into array.
                array[i] = rnd.Next(1, 10);
            }

            return array;
        }

        public static int[,] fill2DArray(int[,] array)
        {
            Random rnd = new Random();

            //Loop through array. (We use array.GetLength(0) with the index of the "dimension" to get the size.)
            for (int i = 0; i < array.GetLength(0); i++)
            {
                for (int j = 0; j < array.GetLength(1); j++)
                {
                    //Save a random number form 1-10 into array.
                    array[i, j] = rnd.Next(1, 10);
                }
            }

            return array;
        }

        public static int[,,] fill3DArray(int[,,] array)
        {
            Random rnd = new Random();

            //Loop through array. (We use array.GetLength(0) with the index of the "dimension" to get the size.)
            for (int i = 0; i < array.GetLength(0); i++)
            {
                for (int j = 0; j < array.GetLength(1); j++)
                {
                    for (int k = 0; k < array.GetLength(2); k++)
                    {
                        //Save a random number form 1-10 into array.
                        array[i, j, k] = rnd.Next(1, 10);
                    }
                }
            }

            return array;
        }
    }
}

Resulting output:

Share:

Leave a Reply

Your email address will not be published. Required fields are marked *

The following GDPR rules must be read and accepted:
This form collects your name, email and content so that we can keep track of the comments placed on the website. For more info check our privacy policy where you will get more info on where, how and why we store your data.

Advertisment ad adsense adlogger