Arrays and Methods


Passing an array to a method

Remember that an array variable really holds a reference to an array. When you pass an array as an argument to a method, you are passing the array reference, allowing the method to refer to that same array. Any changes that the method makes to the array are, of course, seen no matter who is referring to that array.

To illustrate, here is a method that replaces each value in an array by its square. For example, if a variable held 7, it is modified to hold 49.

  public static squareAll(int[] A)
  {
    for(int k = 0; k < A.length; k++) 
    {
      A[k] = A[k] * A[k];
    }
  }
A more useful method might take into account that the logical size of the array could be smaller than the physical size. It takes the logical size as an argument, and only goes up to that size.
  public static squareAll(int[] A, int n)
  {
    for(int k = 0; k < n; k++) 
    {
      A[k] = A[k] * A[k];
    }
  }

Now imagine another place where the second version of squareAll (with two arguments, an array and a size) is used. Program fragment

  int[] frog = new int[10];
  for(int i = 0; i < 10; i++) frog[i] = i;
  squareAll(frog, 10);  
  for(int i = 0; i < 10; i)) 
  {
    System.out.println(frog[i]);
  }
displays
  0
  1
  4
  9
  16
  25
  36
  49
  64
  81  


Returning an array from a method

A method can also return an array as its answer. Here is one that takes a string and returns an array of characters that contains exactly the characters in that string. (The array size is the same as the length of the string.)

  public static makeArray(String s)
  {
    char[] a = new char[s.length()];
    for(int k = 0; k < a.length; k++) 
    {
      a[k] = s.charAt(k);
    }
    return a;
  }

A method that does the same thing is available to you in the string class. If s is a string then s.toArray( ) produces an array of characters holding the characters in string s.


Problems

  1. [solve] Write a method smallest(A,n) that returns the smallest of A[0], ..., A[n-1]. A should have type int[]. Assume that n is at least 1.

  2. [solve] Write a method reverse(A) that takes an array of integers A and reverses its order, using the entire physical size of the array. What was last becomes first and what was first becomes last. Think carefully about how to do this before writing it. Make sure your idea works on an example.

  3. [solve] Write a method addToFront(x, A) that takes an integer x and an array of integers as an argument, and that returns a newly created array holding x at index 0 and holding all members of array A after that, in the same order. For example, if x = 25 and A has size 4 and is as follows

      A[0] = 14
      A[1] = 71
      A[2] = 5
      A[3] = 21
    
    then addToFront returns a new array (let's call it B just to give it a name) containing
      B[0] = 25
      B[1] = 14
      B[2] = 71
      B[3] = 5
      B[4] = 21
    

  4. [solve] Write a method reversal(A) that takes an array A of integers as an argument and returns a new array of the same size holding the same values as A, but in reverse order.