remove an element from an array in java

Problem Statement: 

How to remove an element from an array ?

Solution Approach: 

  • Create an Array List with values
  • Call ArrayList.remove(index)
  • Print the Array List Values
package com.java.planforexams.array;
import java.util.ArrayList;
public class RemoveElementFromArray {
	public static void main(String[] args) {
	ArrayList<Integer> arrayList = new ArrayList<Integer>(3);
	arrayList.add(0,11);
	arrayList.add(1,21);
	arrayList.add(2,31);
    System.out.println("Current Array List"+arrayList);
    
    arrayList.remove(0);
    System.out.println("Array List after removing 1st Index element");
    for (Integer arrayElement : arrayList) {
    	 System.out.println("Element is " +arrayElement);
    }
    arrayList.remove(1);
    System.out.println("Array List after removing  2nd Index element");
    for (Integer arrayElement : arrayList) {
   	 System.out.println("Element is " +arrayElement);
   }
 }
}
Output: 
Current Array List[11, 21, 31]
Array List after removing 1st Index element
Element is 21
Element is 31
Array List after removing  2nd Index element
Element is 21

How to find average of one dimensional array elements ?

The blog provides the technical approach on ” How to find average of one dimensional array elements ?” 

Problem Statement: 

Java program to find average of one dimensional array elements ?

Solution Approach:

  • Get the Array Count as User Input
  • Get the Array Elements as User Input
  • Iterate the Array Elements
  • Get the sum of all array elements
  • Divide the array element sum by total number of elements
package com.java.planforexams.array;
import java.util.Scanner;
public class ArrayElementAverage {
	public static void main(String[] args) {
		// Scanner object for reading User Input
				Scanner scan = new Scanner(System.in);
				int count,arrayElemntSum = 0;
				float arrayElementAverage;
				// Receive the Array Element Count from User
				System.out.print("Provide the Array Element Count : ");
				// Get the Array Count
				count = scan.nextInt();
				//Set the Array Size with Count value
				int arrayInputFromUser[] = new int[count];
				// Receive the Array Elements from User.
				System.out.println("Enter the elements:");
				// Iterate the array elements
				for (int incr=0; incr<count; incr++)
				{
					arrayInputFromUser[incr] = scan.nextInt();
					arrayElemntSum = arrayElemntSum + arrayInputFromUser[incr];
				}
				arrayElementAverage = (float)arrayElemntSum / count;
				System.out.print("Average of the array elements is : " +arrayElementAverage);
	}
}
Output:
Provide the Array Element Count : 6
Enter the elements:
2
12
43
56
7
8
Average of the array elements is : 21.333334