Java

TOPIC 45 – PROCESSING/SEARCHING ARRAYS

 

 

LESSON NOTE

 

 

ARRAYS WITH FUNCTIONS

 

In this lesson, we will process the data in an array.  This means going through the data to analyze it or to search it or to calculate something with it.  We often do this in a function. 

 

So, we will start by learning how to pass arrays to and from functions and details related to doing so.

 

PASSING BY VALUE

 

Whenever an argument such as an integer is passed from a function call to a function, it is said to be passed by value.  That is because in memory, a second copy of that integer’s value is made.  If that value is changed inside the function, it will not affect the original value from where the function call was made.

 

PASSING BY REFERENCE

 

Passing by value is inconvenient for arrays because they can be so large and take up so much memory – you don’t really want to use twice the space to copy all the info over.  Therefore, they are not passed by value, but rather, they are passed by reference.  Instead of making a copy of all the values in the array, all that is copied is the reference (address) to the array.

 

This means that if you pass an array to a function and make changes to it, then those changes will still exist after the function ends.

 

RULES FOR PASSING ARRAYS TO/FROM FUNCTIONS

 

In function calls, we simply use the array name (no square brackets).

In the function’s prototype, we simply declare an array of that type.  So if we are passing an integer array, we need something like
int[] arr in the argument list.

To return an array, we simply use:

 

return arrayName;

 

However, note that it is not common to need to return an array unless we created a new one in the function because they are passed in by reference.  So, if we passed in an array that the function needs to change, the function can change it and not return it.

 

COMMON REASONS FOR PROCESSING/SEARCHING ARRAYS

Processing an array simply means going over all elements in array to figure out some information about the data in the array.  Here are common reasons that we have to process an array:

  • Counting something in an array.
  • Searching for something in an array.
  • Totaling an arrays.

 

TEMPLATE FOR PROCESSING ARRAYS

 

Here is the for loop template to go through an array one element at a time.

 

for (int i = 0; i < arr.length; i++)

{

    //here, arr[i] will take the value of every single element

}

 

TYPES OF SEARCHING

 

There are many different ways that we can search through an array.  All of them require that the programmer be comfortable with the template above.

 

A few ways to search include:

  • Finding the min or max value in the array.
  • Finding out whether or not an array contains a specific value.
  • Finding the number of occurrences of a specific value in the array.
  • Finding the index of a specific value in the array.

 

EXAMPLES

 

The only way to master these challenging algorithms is to try them yourself.  You should first study a few examples to understand them and then try doing them on your own.

 

Example 1A – Write a function called contains that will get an int array and an int value as arguments and return true if the value is found in the array. Otherwise, it returns false.

 

public static boolean contains(int[] a, int v)

{

   for(int x = 0; x < a.length; x++)

   {

      if (a[x] == v)

      {

         return true;

      }

   }

 

   //if we are done loop and no match found, then we return false

   return false;

}


 

Example 1B – In your main (of the same class), create an int array of length 500 and give each element a random number between 1 and 1000. Then use the method above to see if the value 77 is in the array.

 

public static void main(String[] args)

{

   //Create array

   int[] intArray = new int[500];

 

   //Give each element a random number – notice the template

   for (int y = 0; y < intArray.length; y++)

   {

      intArray[y] = (int)(1000* Math.random()) + 1;

   }

 

   //Check if array contains 77

   if(contains(intArray, 77))

   {

      System.out.println(“Yes, the array contains 77.”);

   }

   else

   {

      System.out.println(“No, the array doesn’t contain 77.”);

   }

}

 

EXAMPLE 2 – Write a function that receives an int array as parameter and returns the minimum value in the array.

 

     public static int min(int[] a)

     {

        //assume that the first element is smallest

        int minVal = a[0];  

 

        //check it agains all other elements

        for (int i = 0; i < a.length; i++)

        {

           if (a[i] < minVal)

           {

              minVal = a[i];

           }

        }

        return minVal;

     }