Common Array Operations

7.4 Common Array Operations

Some common operations performed on a one-dimensional array are reading data into the array, printing data, and finding the largest and/ or smallest element in the array. If the data type of an array element is numeric, some common operations are to find the sum and average of the elements of the array. To access the methods to process a one-dimensional array easily, we create the class CommonArrayMethods and put these methods in this class.
(CommonArrayMethods.java)

import java.util.Scanner;

/**
 * This class contains common methods to
 * manipulate array.
 */
public class CommonArrayMethods
{
   /**
    * The fillArray method accepts an array and number of
    * elements as an argument. Each of its Element is filled by user.
    */
   private static void fillArray(int[] list, int n)
   {
      // Create a Scanner object for keyboard input.
      Scanner console = new Scanner(System.in);

      for (int i = 0; i < n; i++)
      {
         list[i] = console.nextInt();
      }
   }

   /**
    * The printArray method accepts an array and number of
    * elements as argument and displays its contents.
    */
   public static void printArray(int[] list, int n)
   {
      for (int i = 0; i < n; i++)
      {
         System.out.print(list[i] + " ");
      }
   }

   /**
    * The sumArray method adds the sum of elements and return it.
    */
   public static int sumArray(int[] list, int n)
   {
      int sum = 0;

      for (int i = 0; i < n; i++)
      {
         sum += list[i];
      }

      return sum;
   }

   /**
    * The largestElement method returns the index of largest element.
    */
   public static int largestElement(int[] list, int n)
   {
      int max = 0;    // store index number of first element

      for (int i = 1; i < n; i++)
      {
         if (list[max] > list[i])
         {
            max = i;
         }
      }

      return max;
   }
}
(CommonArrayMethodsDemo.java)
Because the methods of the class CommonArrayMethods are public and static, they can be called by using the name of the class and the dot operator. (More on Static members of class are discussed in next chapter)
public class CommonArrayMethodsDemo
{
   public static void main(String[] args)
   {
      final int SIZE = 4;    // Size of the array

      // Create an array.
      int[] array = new int[SIZE];

      // Read into array.
      System.out.println("Enter numbers ");
      CommonArrayMethods.fillArray(array, SIZE);

      // Print the array.
      System.out.println("Numbers are :");
      CommonArrayMethods.printArray(array, SIZE);
      System.out.println();

      // Display the sum of the elements of array.
      System.out.println("The sum of the elements is: "
                         + CommonArrayMethods.sumArray(array, SIZE));

      // Display the index of largest element of array.
      System.out.println("Largest element is at index "
                         + CommonArrayMethods.largestElement(array,
                            SIZE));
   }
}          
Output :
Enter numbers
5
8
9
2

Numbers are :
5 8 9 2
The sum of the elements is: 24
Largest element is at index 2

No comments:

Post a Comment

Pages