Saturday 23 June 2012

java program for maintaing array(insert by position,delete by position,update,and display).

import java.io.*;

class ArrayClass
{
    public static void main(String args[]) throws IOException
    {
    int i,j,k,no,top=0,choice,max=0;
    int temp;
    int a[];
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter Total element that you want in array");
    max=Integer.parseInt(br.readLine());
    a=new int[max];
   
   

do
        {
        System.out.println("Select From list:");
        System.out.println("1.Insert");
        System.out.println("2.Delete");
        System.out.println("3.Update");
        System.out.println("4.Display");
        System.out.println("5.Exit");
        System.out.print("Enter Your choice : ");
        choice=Integer.parseInt(br.readLine());

        switch(choice)
            {
            case 1:
                if(top >max-1)
                {
                System.out.println("Overflow");
                break;
                }
               
                System.out.print("enter Element :");
                 temp=Integer.parseInt(br.readLine());
                a[top]=temp;
                top=top+1;
                break;
            case 2:
                if(top <= 0)
                {
                System.out.println("Under Flow");
                break;
                }
                System.out.print("Enter Your positon that you want to delete : ");
               
                temp=Integer.parseInt(br.readLine());
                if(temp>max || temp<= 0)
                {
                System.out.println("Invalid input");
                break;
                }
               
                for(i=temp-1;i<top;i++)
                {
                a[i]=a[i+1];
                }
                top=top-1;
                break;
              case 3:
                if(top <= 0)
                {
                System.out.println("No element is available");
                break;
                }
                System.out.println("Enter Your positon that you want to update ");
                 temp=Integer.parseInt(br.readLine());
                System.out.println("Enter Your new value ");
                int  temp1=Integer.parseInt(br.readLine());
                a[temp-1]=temp1;
                break;
            case 4:
                if(top <= 0)
                {
                System.out.println("No element is available");
                break;
                }
                System.out.println("Your Array is: ");
                for(i=0;i< top;i++)
                {               
                System.out.println("Element at"+(i+1) +":" +a[i]);
                }
                break;
            case 5:
                break;
        }
    }while(choice != 5);

}
}

No comments:

Post a Comment