Startertutorials Blog
Tutorials and articles related to programming, computer science, technology and others.
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.
Home » Programming » Java » Programs » Arrays » Basic array programs in Java
Suryateja Pericherla Categories: Arrays. No Comments on Basic array programs in Java
2
(1)

In this post we are going to look at some very basic and frequently asked array programs in Java interviews

.

Different programs are written as separate functions in the class ArrayClass. These programs are restricted to integers. Following programs are available:

  • Reading elements into an array
  • Printing elements in an array
  • Printing the sum of all elements in an array
  • Printing the average of elements in an array
  • Print the least element in the array
  • Print the largest element in the array
  • Search an element in the array using linear search
  • Search an element in the array using binary search
  • Return the frequency or how many times an element is repeated in the array
  • Return the frequency of all elements in the array

 

I have written a function for each of the above program along with a member array variable in the class ArrayClass as shown below:

ArrayClass.java

import java.util.*;
public class ArrayClass
{
	int[] a;
	
	//Method for reading elements in to an array
	public void readArray(int n)
	{
		System.out.println("Enter " + n + " elements: ");
		Scanner input = new Scanner(System.in);
		for(int i = 0; i < n; i++)
		{
			a[i] = input.nextInt();
		}
	}
	
	//Method for printing elements in an array
	public void printArray(int n)
	{
		System.out.println("Array elements are: ");
		for(int i = 0; i < n; i++)
		{
			System.out.print(a[i] + " ");
		}
		System.out.print("\n");
	}
	
	//Method to return the sum of all elements in the array
	public int sumArray(int n)
	{
		int sum = 0;
		for(int i = 0; i < n; i++)
			sum = sum + a[i];
		return sum;
	}
	
	//Method to return the average of all elements in the array
	public float avgArray(int n)
	{
		float average = 0.0f;
		average = (float)sumArray(n) / n;
		return average;
	}
	
	//Method to return the least element in the array
	public int leastElement(int n)
	{
		int min = a[0];
		for(int i = 0; i < n; i++)
		{
			if(min > a[i])
			{
				min = a[i];
			}
		}
		return min;
	}
	
	//Method to return the largest element in the array
	public int largestElement(int n)
	{
		int max = a[0];
		for(int i = 0; i < n; i++)
		{
			if(max < a[i])
			{
				max = a[i];
			}
		}
		return max;
	}
	
	//Method to search for an array element using linear search
	public int linearSearch(int key, int n)
	{
		int index = -1;
		for(int i = 0; i < n; i++)
		{
			if(a[i] == key)
			{
				index = i;
				break;
			}
		}
		return index;
	}
	
	//Method to search for an array element using binary search
	public int binarySearch(int key, int n)
	{
		int index = -1;
		int low = 0, high = n-1;
		while(low <= high)
		{
			int mid = (low + high) / 2;
			if(key == a[mid])
			{
				index = mid;
				break;
			}
			else if(key < a[mid])
			{
				high = mid - 1;
			}
			else
			{
				low = mid + 1;
			}
		}
		return index;
	}
	
	//Method to return the frequency of a given element
	public int getFrequency(int key, int n)
	{
		int count = 0;
		for(int i = 0; i < n; i++)
		{
			if(a[i] == key)
			{
				count++;
			}
		}
		return count;
	}
	
	//Method to return the frequency of all elements in the array where elements are in the range 1-n
	//Step 1: Decrement each element by 1 to make the elements to be in the range 0 to n - 1
	//Step 2: Add n to keep track of the frequency of an element. arr[arr[i]%n] = arr[arr[i]%n] + n
	//Step 3: Print the frequency of elements by dividing the values by n
	public void getFrequencyOpt(int[] arr, int n)
	{
		for(int i = 0; i < n; i++)
			arr[i] = arr[i] - 1;
		for(int i = 0; i < n; i++)
			arr[arr[i]%n] = arr[arr[i]%n] + n;
		System.out.println("Frequency of elements are: ");
		for(int i = 0; i < n; i++)
			System.out.println((i + 1) + " -> " + (arr[i] / n));
	}
}

 

Here is the driver program to execute the above class:


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


Driver.java

import java.util.*;
class Driver
{
	public static void main(String[] args)
	{
		ArrayClass arrayObj = new ArrayClass();
		Scanner input = new Scanner(System.in);
		System.out.println("Enter no. of elements: ");
		int n = input.nextInt();
		arrayObj.a = new int[n];
		arrayObj.readArray(n);
		arrayObj.printArray(n);
		System.out.println("Length of the array is: " + arrayObj.a.length);
		System.out.println("Sum of elements in the array is: " + arrayObj.sumArray(n));
		System.out.println("Average of elements in the array is: " + arrayObj.avgArray(n));
		System.out.println("Least element in the array is: " + arrayObj.leastElement(n));
		System.out.println("Largest element in the array is: " + arrayObj.largestElement(n));
		System.out.println("Enter a element to search: ");
		int key = input.nextInt();
		int index = arrayObj.linearSearch(key, n);
		if(index != -1)
			System.out.println("Linear search: Given element is found at index: " + index);
		else
			System.out.println("Linear search: Given element is not found");
		index = arrayObj.binarySearch(key, n);
		if(index != -1)
			System.out.println("Binary search: Given element is found at index: " + index);
		else
			System.out.println("Binary search: Given element is not found");
		System.out.println("Enter a element to get its frequency: ");
		key = input.nextInt();
		System.out.println("Frequency of the given element is: " + arrayObj.getFrequency(key, n));
		arrayObj.getFrequencyOpt(arrayObj.a, n);
	}
}

 

Input and output of the above program is as given below:

Enter no. of elements:
6
Enter 6 elements:
1 1 2 3 3 5
Array elements are:
1 1 2 3 3 5
Length of the array is: 6
Sum of elements in the array is: 15
Average of elements in the array is: 2.5
Least element in the array is: 1
Largest element in the array is: 5
Enter a element to search:
5
Linear search: Given element is found at index: 5
Binary search: Given element is found at index: 5
Enter a element to get its frequency:
3
Frequency of the given element is: 2
Frequency of elements are:
1 -> 2
2 -> 1
3 -> 2
4 -> 0
5 -> 1
6 -> 0

 

If you need any further explanation, comment below and I will answer.

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Leave a Reply

Your email address will not be published. Required fields are marked *

Facebook
Twitter
Pinterest
Youtube
Instagram
Blogarama - Blog Directory