Skip to content

Instantly share code, notes, and snippets.

@imranhoshain
Created September 6, 2022 14:45
Show Gist options
  • Save imranhoshain/4ab617ac49863ac3af96134c9ee12b71 to your computer and use it in GitHub Desktop.
Save imranhoshain/4ab617ac49863ac3af96134c9ee12b71 to your computer and use it in GitHub Desktop.
package arrayinsdel;
import java.util.Scanner;
/**
*
* @author Spy Hacker
*/
public class ArrayInsDel {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner scObj = new Scanner(System.in);
int a[] = new int[100];
System.out.print("Enter the size of array: ");
int size = scObj.nextInt();
System.out.println("Enter the array of element: ");
for (int i=0; i<size; i++){
a[i] = scObj.nextInt();
}
//Insertion
System.out.print("Array before insertion: ");
for (int i = 0; i < size; i++) {
System.out.print(a[i]+", ");
}
System.out.print("\nEnter the element to be inserted: ");
int x = scObj.nextInt();
System.out.print("Enter the position you want to insert: ");
int pos = scObj.nextInt();
for (int i = size; i > pos; i--) {
a[i] = a[i-1];
}
a[pos] = x;
++size;
System.out.print("Array after insertion: ");
for (int i = 0; i < size; i++) {
System.out.print(a[i]+", ");
}
//Deletion
System.out.print("\nEnter the position to be delete: ");
int delpos = scObj.nextInt();
for (int i = delpos; i < size; i++) {
a[i] = a[i+1];
}
--size;
System.out.print("Array after delection: ");
for (int i = 0; i < size; i++) {
System.out.print(a[i]+", ");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment