Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Rearrange
- {
- static Scanner sc = new Scanner(System.in);
- int a[];
- int n;
- int capacity;
- int pos1;
- int pos2;
- int item;
- void enter()
- {
- // Maximum number of elements that the array
- // can store
- capacity = 1000;
- a = new int[capacity];
- System.out.print("Enter the size of the array: ");
- n = sc.nextInt();
- for(int i = 0; i < n; i++)
- {
- System.out.print("Enter element " + (i + 1) + ": ");
- a[i] = sc.nextInt();
- }
- }
- void insert()
- {
- // Take a valid position as input
- if(n == capacity)
- {
- System.out.println("Array does not have space to insert more elements");
- }
- else
- {
- while(true)
- {
- System.out.println("Valid positions for insertions are");
- System.out.println(0 + " to " + n);
- System.out.print("Enter the position to insert element: ");
- pos1 = sc.nextInt();
- if(pos1 >= 0 && pos1 <= n)
- {
- break;
- }
- else
- {
- System.out.println(pos1 + " is an invalid position.");
- }
- }
- System.out.print("Enter the element to be inserted in array: ");
- item = sc.nextInt();
- // Shift all elements from positions
- // pos1 to n-1, one place to the right
- for(int i = n-1; i >= pos1; i--)
- {
- a[i + 1] = a[i];
- }
- // Insert the new element
- a[pos1] = item;
- // Increase the size of the array
- n = n + 1;
- System.out.println(item + " inserted at position " + pos1);
- System.out.println("New contents of the array are");
- disp1();
- }
- }
- void remove()
- {
- if(n == 0)
- {
- System.out.println("Cannot delete from an empty array.");
- }
- else
- {
- while(true)
- {
- System.out.println("Valid positions for insertions are");
- System.out.println(0 + " to " + (n-1));
- System.out.print("Enter the position to delete an element: ");
- pos2 = sc.nextInt();
- if(pos2 >= 0 && pos2 <= (n - 1))
- {
- break;
- }
- else
- {
- System.out.println(pos2 + " is an invalid position.");
- }
- }
- // To delete the element at pos2 position,
- // all elements from pos2 + 1 to n-1 must be shifted by
- // one place to the left
- int deletedElement = a[pos2];
- for(int i = pos2 + 1; i < n; i++)
- {
- a[i - 1] = a[i];
- }
- // Decrease size of array
- n = n - 1;
- System.out.println(deletedElement + " deleted from position " + pos2);
- System.out.println("New contents of the array are");
- disp2();
- }
- }
- void disp1()
- {
- for(int i = 0; i < n; i++)
- {
- System.out.print(a[i] + ", ");
- }
- System.out.println("");
- }
- void disp2()
- {
- disp1();
- }
- public static void main(String[] args)
- {
- Rearrange obj = new Rearrange();
- obj.enter();
- int choice;
- do
- {
- System.out.println("---------Menu---------");
- System.out.println("Press 1 for inserting an element in the array");
- System.out.println("Press 2 for deleting an element from the array");
- System.out.println("Press 3 for displaying the contents of the array");
- System.out.println("Press 4 to quit");
- System.out.print("Enter your choice(1-4): ");
- choice = sc.nextInt();
- switch(choice)
- {
- case 1: {
- obj.insert();
- break;
- }
- case 2: {
- obj.remove();
- break;
- }
- case 3: {
- obj.disp1();
- break;
- }
- case 4: {
- System.out.println("Quitting...");
- break;
- }
- default: {
- System.out.println("Invalid choice.");
- }
- }
- }while(choice != 4);
- }
- }
Add Comment
Please, Sign In to add comment