import java.util.*;
// a menu driven program to test a selection of useful array methods
public class SomeUsefulArrayMethods
{
public static void main (String[] args)
{
char choice;
Scanner sc = new Scanner(System.in);
int[] someArray; // declare an integer array
System.out.print("How many elements to store?: ");
int size = sc.nextInt();
// size the array
someArray = new int [size];
// menu
do
{
System.out.println();
System.out.println("[1] Enter values");
System.out.println("[2] Array maximum");
System.out.println("[3] Array sum");
System.out.println("[4] Array membership");
System.out.println("[5] Array search");
System.out.println("[6] Display values");
System.out.println("[7] Exit");
System.out.print("Enter choice [1-7]: ");
choice = sc.next().charAt(0);
System.out.println();
// process choice by calling helper methods
switch(choice)
{
case '1': fillArray(someArray);
break;
case '2': int max = max(someArray);
System.out.println("Maximum array value = " + max);
break;
case '3': int total = sum(someArray);
System.out.println("Sum of array values = " + total);
break;
case '4': System.out.print ("Enter value to find: ");
int value = sc.nextInt();
boolean found = contains(someArray, value);
if (found)
{
System.out.println(value + " is in the array");
}
else
{
System.out.println(value + " is not in the array");
}
break;
case '5': System.out.print ("Enter value to find: ");
int item = sc.nextInt();
int index = search(someArray, item);
if (index == -999) // indicates value not found
{
System.out.println
("This value is not in the array");
}
else
{
System.out.println
("This value is at array index " + index);
}
break;
case '6': System.out.println("Array values");
displayArray(someArray);
break;
}
} while (choice != '7');
System.out.println("Goodbye");
}
// helper methods
// fills an array with values
public static void fillArray(int[] arrayIn)
{
Scanner sc = new Scanner (System.in);
for (int i = 0; i < arrayIn.length; i++)
{
System.out.print("enter value ");
arrayIn[i] = sc.nextInt();
}
}
// returns the total of all the values held within an array
private static int sum (int[] arrayIn)
{
int total = 0;
for (int currentElement : arrayIn)
{
total = total + currentElement;
}
return total;
}
// returns the maximum value in an array
private static int max (int[] arrayIn)
{
int result = arrayIn[0];
for (int currentElement : arrayIn)
{
if (currentElement > result)
{
result = currentElement;
}
}
return result;
}
// checks whether or not an item is contained within an array
private static boolean contains (int[] arrayIn, int valueIn)
{
for (int currentElement : arrayIn)
{
if (currentElement == valueIn)
{
return true;
}
}
return false;
}
/* returns the position of an item within an array
or -999 if the value is not present within the array */
private static int search (int[] arrayIn, int valueIn)
{
for (int i=0; i < arrayIn.length; i++)
{
if (arrayIn[i] == valueIn)
{
return i;
}
}
return -999;
}
// displays the array values on the screen
public static void displayArray(int[] arrayIn)
{
System.out.println();
// standard 'for' loop used here as the array index is required
for (int i = 0; i < arrayIn.length; i++)
{
System.out.println("array[" + i + "] = " + arrayIn[i]);
}
}
}