Arrays as Arguments

Passing Single-Dimensional Arrays As Arguments

For example, the following statement sends an array to a print method.


class MainClass
{
    public static void Main()
    {
        int[] theArray = { 1, 3, 5, 7, 9 };
        PrintArray(theArray);


    }
    public static void PrintArray(int[] arr)
    {
        foreach (int i in arr)
        {
            System.Console.WriteLine(i);
        }
    }

}

The output:


1
3
5
7
9

You can initialize and pass a new array in one step, as is shown in the following example.


class MainClass
{
    public static void Main()
    {
         PrintArray(new int[] { 1, 3, 5, 7, 9 });
    }
    static void PrintArray(int[] arr)
    {
        foreach(int i in arr){
            System.Console.WriteLine(i);
        }
    }

}

The output:


1
3
5
7
9

In the following example, an array of strings is initialized and passed as an argument.

 
class MainClass
{
    static void PrintArray(string[] arr)
    {
        for (int i = 0; i < arr.Length; i++)
        {
            System.Console.Write(arr[i] + "{0}", i < arr.Length - 1 ? " " : "");
        }
    }

    static void Main()
    {
        // Declare and initialize an array.
        string[] weekDays = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };

        // Pass the array as an argument to PrintArray.
        PrintArray(weekDays);
    }
}

The output:


Sun Mon Tue Wed Thu Fri Sat

Passing Multidimensional Arrays As Arguments


class MainClass
{
    public static void Main()
    {

        int[,] theArray = { { 1, 2 }, { 2, 3 }, { 3, 4 } };
        Print2DArray(theArray);
    }

    public static void Print2DArray(int[,] arr)
    {
        foreach (int a in arr)
        {
            System.Console.WriteLine(a);
        }
    }
}

The output:


1
2
2
3
3
4

You can initialize and pass a new array in one step, as is shown in the following example.


class MainClass
{
    public static void Main()
    {

        Print2DArray(new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } });
    }

    public static void Print2DArray(int[,] arr)
    {
        foreach (int a in arr)
        {
            System.Console.WriteLine(ar);
        }
    }
}

The output:


1
2
3
4
5
6
7
8

In the following example, a two-dimensional array of integers is initialized and passed to the Print2DArray method.

The method displays the elements of the array.

 
class MainClass
{
    static void Print2DArray(int[,] arr)
    {
        // Display the array elements.
        for (int i = 0; i < arr.GetLength(0); i++)
        {
            for (int j = 0; j < arr.GetLength(1); j++)
            {
                System.Console.WriteLine("Element({0},{1})={2}", i, j, arr[i, j]);
            }
        }
    }
    static void Main()
    {
        // Pass the array as an argument.
        Print2DArray(new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } });
    }
}

The output:


Element(0,0)=1
Element(0,1)=2
Element(1,0)=3
Element(1,1)=4
Element(2,0)=5
Element(2,1)=6
Element(3,0)=7
Element(3,1)=8
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.