Methods that return an array reference

  • Methods in Java can return an array (reference) data type

  • Structure of methods that returns an array (reference) data type:

        public static returnType[] methodName( .... )
        {
            /* -----------------------------------------
               (1) You must create an array to return
    	   ------------------------------------------ */
            returnType[] result = new returnType[N];
    
    	// The method body would fill the
    	// result array with the desired values
    
    	... (body of the method) ... 
    
    
    	/* -----------------------------------------
    	   (2) Return the result array
    	   ----------------------------------------- */
    	return result;
        }
      

Methods that return an array reference   example

Write a reverse( ) method that (1) reverses the elements in its input list[] and (2) returns the result array:

    public static void main(String[] args)
    {
        int[] myList = {1, 2, 3, 4};
	int[] out;                     // Output of algorithm
        
        out = reverse(myList); 
        for ( int i = 0; i < out.length; i++)
            System.out.print(out[i] + " ");
    }

    public static int[] reverse(int[] list) 
    {






    } 

Methods that return an array reference   example

(1) Create an array to store the result

    public static void main(String[] args)
    {
        int[] myList = {1, 2, 3, 4};
	int[] out;                     // Output of algorithm
        
        out = reverse(myList); 
        for ( int i = 0; i < out.length; i++)
            System.out.print(out[i] + " ");
    }

    public static int[] reverse(int[] list) 
    {
        int[] result = new int[list.length];





    } 

Methods that return an array reference   example

(2) To reverse the elements in the input array, we copy the elements to the result array in reverse order:

    public static void main(String[] args)
    {
        int[] myList = {1, 2, 3, 4};
	int[] out;                     // Output of algorithm
        
        out = reverse(myList); 
        for ( int i = 0; i < out.length; i++)
            System.out.print(out[i] + " ");
    }

    public static int[] reverse(int[] list) 
    {
        int[] result = new int[list.length];

           
    } 

Methods that return an array reference   example

(2) This for-loop will copy the elements in the list array in the reverse order to result array:

    public static void main(String[] args)
    {
        int[] myList = {1, 2, 3, 4};
	int[] out;                     // Output of algorithm
        
        out = reverse(myList); 
        for ( int i = 0; i < out.length; i++)
            System.out.print(out[i] + " ");
    }

    public static int[] reverse(int[] list)
    {
        int[] result = new int[list.length];

        for (int i = 0; i < list.length; i++) 
            result[(list.length-1) - i] = list[i];
            

    } 

Methods that return an array reference   example

(3) Return the result

    public static void main(String[] args)
    {
        int[] myList = {1, 2, 3, 4};
	int[] out;                     // Output of algorithm
        
        out = reverse(myList); 
        for ( int i = 0; i < out.length; i++)
            System.out.print(out[i] + " ");
    }

    public static int[] reverse(int[] list)
    {
        int[] result = new int[list.length];

        for (int i = 0; i < list.length; i++) 
            result[(list.length-1) - i] = list[i];
            
        return result;
    } 

DEMO: demo/08-array/10-return-array/ReturnArray.java