Day - 2 - Lab Content - Array Operations Using Java
Day - 2 - Lab Content - Array Operations Using Java
Bengaluru, Karnataka
Computer Science & Engineering
School of Computer Science & Engineering
Prepared by,
Ms. Sridevi S, AP/SoCSE, Ms. Meena Kumari, AP/SoCSE, Ms. Rohini A, AP/SoCSE 1
Output:
1 2 3 4 5 0 0 0 0 0
10 20 30 40 50
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]
After adding this code in main program the resultant final code for array traversal and
inserting element at given position is:
import java.util.*;
public class read_array
{
public static void main(String args[ ])
{
Scanner sc=new Scanner(System.in);
int n, i; //variable n is used to store size of array, variable i is used indexing
int a[]=new int[10]; // array declaration in java language
int pos, value;
System.out.println("Enter the size of array");
n=sc.nextInt();
System.out.println("Enter the array elements");
for(i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
System.out.println("The array elements are:");
for(i=0;i<n;i++)
{
System.out.println(a[i]);
}
System.out.println("Enter the position to insert new element");
Prepared by,
Ms. Sridevi S, AP/SoCSE, Ms. Meena Kumari, AP/SoCSE, Ms. Rohini A, AP/SoCSE 3
pos=sc.nextInt();
System.out.println("Enter the value");
value=sc.nextInt();
for(i=n-1;i>=pos-1;i--)
{
a[i+1]=a[i];
}
a[pos-1]=value;
n++;
System.out.println("The array elements are inserting new element is:");
for(i=0;i<n;i++)
{
System.out.println(a[i]);
}
}
}
Output:
Prepared by,
Ms. Sridevi S, AP/SoCSE, Ms. Meena Kumari, AP/SoCSE, Ms. Rohini A, AP/SoCSE 4
Logic:
1. Push existing element at position 5 (index value=4) to position 6 (index value=5)
10 20 30 40 50
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]
10 20 30 40 50
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]
10 20 30 40 50
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]
for(i=n-1;i>=0;i--)
a[i+1]=a[i];
import java.util.*;
public class read_array
{
public static void main(String args[ ])
{
Scanner sc=new Scanner(System.in);
int n, i; //variable n is used to store size of array, variable i is used indexing
int a[]=new int[10]; // array declaration in java language
int pos, value;
System.out.println("Enter the size of array");
n=sc.nextInt();
Prepared by,
Ms. Sridevi S, AP/SoCSE, Ms. Meena Kumari, AP/SoCSE, Ms. Rohini A, AP/SoCSE 5
System.out.println("Enter the array elements");
for(i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
System.out.println("The array elements are:");
for(i=0;i<n;i++)
{
System.out.println(a[i]);
}
System.out.println("Enter the position to insert new element");
pos=sc.nextInt();
System.out.println("Enter the value");
value=sc.nextInt();
for(i=n-1;i>=0;i--) //Only the highlighted condition is changed
{
a[i+1]=a[i];
}
a[pos-1]=value;
n++;
System.out.println("The array elements are inserting new element is:");
for(i=0;i<n;i++)
{
System.out.println(a[i]);
}
}
}
Prepared by,
Ms. Sridevi S, AP/SoCSE, Ms. Meena Kumari, AP/SoCSE, Ms. Rohini A, AP/SoCSE 6
Output:
Prepared by,
Ms. Sridevi S, AP/SoCSE, Ms. Meena Kumari, AP/SoCSE, Ms. Rohini A, AP/SoCSE 7
}
System.out.println("The array elements are:");
for(i=0;i<n;i++)
{
System.out.println(a[i]);
}
System.out.println("Enter the value");
value=sc.nextInt();
a[i]=value;
n++;
System.out.println("The array elements are inserting new element is:");
for(i=0;i<n;i++)
{
System.out.println(a[i]);
}
}
}
Output:
Prepared by,
Ms. Sridevi S, AP/SoCSE, Ms. Meena Kumari, AP/SoCSE, Ms. Rohini A, AP/SoCSE 8