Defining 2-dimensional arrays as arguments to a method

  • 2 dimensional arrays can be used as parameters to a method as follows:

        dataType[][] arrayName
    

  • Example: a method that computes the sum of all elements in a 2 dimensional array:

        public static int sum(int[][] m) 
        {
            int total = 0;
    
    	for (int i = 0; i < m.length; i++) 
    	    for (int j = 0; j < m[i].length; j++)
    	        total = total + m[i][j];
    
            return total;
        }  

Passing 2-dimensional arrays as arguments to a method

  • 2-dimensional arrays can be passed as arguments to methods as follows:

      int[][] myGrid = ...  // initialize the 2-dim array
    
      int s = sum(myGrid);  // Call sum wiith argument myGrid
    

    Example:

        public static void main(String[] args)
        {
            int[][] myGrid = { {1,2,3},
                               {4,5},
                               {6,7,8,9} };
            int s;
           
            s = sum(myGrid);
            System.out.println( s );
        }

DEMO: demo/09-multi-dim-array/04-array-argument/SumArray.java

Remember:    Java passes an array reference to a method

  • In Java, an array is passed to a method in the following manner:

      • When you invoke a method with an array argument, the array reference in the argument is copied (= assigned) to the parameter variable.

  • That means:   the parameter variable will point to the same array object:

    Schematically:

            

Review:   In Java, a method can update the elements in an array parameter

In Java, methods can change/update the elements in an array parameter:

    public static void main(String[] args)
    {
        int[][] myGrid = { {1,2,3}, 
                           {4,5},   
                           {6,7,8,9} };

        increment(myGrid);  // This method will update elements in myGrid

        System.out.println( Arrays.toString(myGrid[0]) ); // [2,3,4]    changed !
        System.out.println( Arrays.toString(myGrid[1]) ); // [5,6]      changed !
        System.out.println( Arrays.toString(myGrid[2]) ); // [7,8,9,10] changed !
    }

    public static void increment(int[][] m) 
    {

	for (int i = 0; i < m.length; i++) 
	    for (int j = 0; j < m[i].length; j++)
	        m[i][j]++;;
    }  

DEMO: demo/09-multi-dim-array/04-array-argument/UpdateArray.java

Methods that return a 2-dimensional array

  • Methods can return a 2-dimensional array

  • It works the same way as methods that return an 1-dimensional array

    Except:   you must define the return type as datatype[][]

          public static datatype[][] methodName( ... )
          {
              // (1) Create a 2-dimensional array to store the result
              datatype[][] result = new datatype[M][N];
      
      	.... // fill result[][] with desired output values
      
              // (2) Return the 2-dimensional array
              return result;
          }


  • We will see an example in the next slide when we discuss how 2-dimensional arrays are used in Mathematics