Hello guys , welcome to Lingarajtechhub.com. Today we are going to discuss Input n elements into an array and insert a new element at a particular position using java.
package com.array.java; import java.util.Scanner; public class Insert_new_element { 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]+","); } } }
Line-1:
❖ Package com.array.java;
It is a java package( com.array.java) which is a collection of classes and this package is created in a java project .
Line-2:
❖ import java.util.Scanner;
Scanner is a class in java. util package used for obtaining the input of the primitive types like int, double, etc. and strings .
Line-4:
❖ public class Add_element{
*Here class Add_element is defined as a class which is a template used to create objects and to define object data types and methods .
>–and it’s defined as public because it can be accessed anywhere .
Line-5:
❖ public static void main(String[] args)
>>– it is a main method in which we can execute a program //public :it is an access modifier of the main function which must be public so that the JVM which is (Java Virtual Machine)can access and execute this method.
//If a method is not public, then access is restricted and its showing error //static is a keyword in which main method has must to be static in which JVM can load the class into memory and call the main method without creating an instance
//void it means no return type
//main() is function string[]args : string means collection of characters ,String[] args represents a collection of Strings that are separated by a space and can be typed into the program on the terminal directly.
Line-7:
❖ int pos,n,x;
>>it means we declare the position, no of elements and the new element .
Line-8:
❖ Scanner sc=new Scanner(System.in);
>>–it means here scanner is a class and Sc is the instance of the object which is new Scanner.System.in is an argument
Line-9:
❖ System.out.println(“enter no of elements”);
>>– it means System is a class name and out is an instance of the System println is print a new line .
Line-10:
- n=sc.nextInt();
>>– here n is a variable that we take and Sc is our instance name which is created for an object and nextInt() is a method method of a Scanner object that reads in a string of digits or char and converts them into an int type.
Line-11:
❖ int a[] =new int[n+1] ;
>>– here we declare array size and initialise the array element which is integer type.
Line-12:
❖ System.out.println(“enter array Elements:\n”);
>>– it means System is a class name and out is an instance of the System println is print a new line .
Line-13:
❖ for(int i=0;i<n;i++) {
>>it means loop through the array by incrementing the value of i.
Line-15:
❖ a[i]=Sc.nextInt();
>>– here i is a variable that we take and Sc is our instance name which is created for an object and nextInt() is a method method of a Scanner object that reads in a string of digits or char and converts them into an int type.
Line-17:
❖ System.out.println(“Enter the position where you want to insert the element”);
>>– it means System is a class name and out is an instance of the System println is print a new line .
>>– here we have to enter the position where we want to insert the new element.
Line-18:
❖ pos=Sc.nextInt();
>>– here pos is a variable that we take and Sc is our instance name which is created for an object and nextInt() is a method method of a Scanner object that reads in a string of digits or char and converts them into an int type.
Line-19:
❖ System.out.println(“enter the element you want to insert”);
>>– it means System is a class name and out is an instance of the System println is print a new line .
>>–here we write the new no which we want to insert.
Line-20:
❖ x=Sc.nextInt();
>>– here x is a variable that we take and Sc is our instance name which is created for an object and nextInt() is a method method of a Scanner object that reads in a string of digits or char and converts them into an int type.
Line-21:
❖ for(int i=(n-1);i>=(pos-1);i- -)
>>–it means loop through the array by decreasing the value of i.
Line-26:
❖System.out.println(“after inserting”);
>>– it means System is a class name and out is an instance of the System println is print a new line .
>>–here after inserting the new value.
Line-27:
❖ for(int i=0;i<n;i++) {
>>it means loop through the array by incrementing the value of i.
Line-29:
❖ System.out.println(a[i]+” “)
>>– it means System is a class name and out is an instance of the System println is print a new line .
>>– it prints the new element in the position where you want to insert it.