Experiment 9
Write a program to sum values of an Single Dimensional array.
1. public class SumOfArray {
2. public static void main(String[] args) {
3. //Initialize array
4. int [] arr = new int [] {1, 2, 3, 4, 5};
5. int sum = 0;
6. //Loop through the array to calculate sum of elements
7. for (int i = 0; i < arr.length; i++) {
8. sum = sum + arr[i];
9. }
10. System.out.println("Sum of all the elements of an array: " + sum);
11. }
12. }
Experiment 10
Design & execute a program in Java to sort a numeric array and a string array.
import java.util.Arrays;
public class Exercise1 {
public static void main(String[] args){
int[] my_array1 = {
1789, 2035, 1899, 1456, 2013,
1458, 2458, 1254, 1472, 2365,
1456, 2165, 1457, 2456};
String[] my_array2 = {
"Java",
"Python",
"PHP",
"C#",
"C Programming",
"C++"
};
System.out.println("Original numeric array : "+Arrays.toString(my_array1));
Arrays.sort(my_array1);
System.out.println("Sorted numeric array : "+Arrays.toString(my_array1));
System.out.println("Original string array : "+Arrays.toString(my_array2));
Arrays.sort(my_array2);
System.out.println("Sorted string array : "+Arrays.toString(my_array2));
Experiment 11
Calculate the average value of array elements through Java Program.
public class Exercise11 {
public static void main(String[] args) {
int[] numbers = new int[]{20, 30, 25, 35, -16, 60, -100};
//calculate sum of all array elements
int sum = 0;
for(int i=0; i < numbers.length ; i++)
{
sum = sum + numbers[i];
//calculate average value
double average = sum / numbers.length;
System.out.println("Average value of the array elements is : " + average);
Experiment 12
Write a Java program to test if an array contains a specific value.
class Main {
public static void main(String[] args) {
int[] num = {1, 2, 3, 4, 5};
int toFind = 3;
boolean found = false;
for (int n : num) {
if (n == toFind) {
found = true;
break;
if(found)
System.out.println(toFind + " is found.");
else
System.out.println(toFind + " is not found.");
Experiment 13
Java program to find index of an element in an array
import java.util.*;
public class index {
// Linear-search function to find the index of an element
public static int findIndex(int arr[], int t)
// if array is Null
if (arr == null) {
return -1;
// find length of array
int len = arr.length;
int i = 0;
// traverse in the array
while (i < len) {
// if the i-th element is t
// then return the index
if (arr[i] == t) {
return i;
else {
i = i + 1;
return -1;
// Driver Code
public static void main(String[] args)
int[] my_array = { 5, 4, 6, 1, 3, 2, 7, 8, 9 };
// find the index of 5
System.out.println("Index position of 5 is: "
+ findIndex(my_array, 5));
// find the index of 7
System.out.println("Index position of 7 is: "
+ findIndex(my_array, 7));
}
Experiment 14
Java program to to remove a specific element from an array
package com.journaldev.java;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] arr = new int[]{1,2,3,4,5};
int[] arr_new = new int[arr.length-1];
int j=3;
for(int i=0, k=0;i<arr.length;i++){
if(arr[i]!=j){
arr_new[k]=arr[i];
k++;
System.out.println("Before deletion :" + Arrays.toString(arr));
System.out.println("After deletion :" + Arrays.toString(arr_new));
}
Experiment 15
Design a program to copy an array by iterating the array.
public class Test {
public static void main(String[] args)
int a[] = { 1, 8, 3 };
// Create an array b[] of same size as a[]
int b[] = new int[a.length];
// Doesn't copy elements of a[] to b[],
// only makes b refer to same location
b = a;
// Change to b[] will also reflect in a[]
// as 'a' and 'b' refer to same location.
b[0]++;
System.out.println("Contents of a[] ");
for (int i = 0; i < a.length; i++)
System.out.print(a[i] + " ");
System.out.println("\n\nContents of b[] ");
for (int i = 0; i < b.length; i++)
System.out.print(b[i] + " ");
}
Experiment 16
import java.util.Scanner;
public class Insert_Array
public static void main(String[] args)
int n, pos, x;
Scanner s = new Scanner(System.in);
System.out.print("Enter no. of elements you want in array:");
n = s.nextInt();
int a[] = new int[n+1];
System.out.println("Enter all the elements:");
for(int i = 0; i < n; i++)
a[i] = s.nextInt();
System.out.print("Enter the position where you want to insert element:");
pos = s.nextInt();
System.out.print("Enter the element you want to insert:");
x = s.nextInt();
for(int i = (n-1); i >= (pos-1); i--)
a[i+1] = a[i];
a[pos-1] = x;
System.out.print("After inserting:");
for(int i = 0; i < n; i++)
System.out.print(a[i]+",");
System.out.print(a[n]);