Logo 
Search:

Java Articles

Submit Article
Home » Articles » Java » Data file structureRSS Feeds

Add,delete,Update in LISTS

Posted By: Falak Khan     Category: Java     Views: 39540

Code for Add,delete,Update in LISTS in Java

/* * To change this template, choose Tools | Templates * and open the template in the editor. */
package sp10.bsb.pkg013;

import java.util.*;
import java.util.Scanner;
/** * * @author Hira */publicclass SP10BSB013 {
      static  List<Integer> l=new ArrayList<Integer>();
    /**     * @param args the command line arguments     */publicstaticvoid main(String[] args) {
        Scanner in=new Scanner(System.in);
     boolean y=true;  
     do{   
         System.out.println("Enter 1 to add a no.(non zero) in list\n2 to edit no.\n3 to search");
         System.out.println("4 to delete\n5 to check total no.\n6 to print list\n7 to exit");
         System.out.println("enter your choice");
         int choice=in.nextInt();
         switch(choice)
         {
            case 1:
                insert();
                break;
            case 2:
                edit();
                break;
            case 3:
                search();
                break;
            case 4:
                delete();
                break;
            case 5:
                total();
                break;
            case 6:
                print();
                break;
            case 7:
                toexit();
                break;
            default:
                System.out.println("bad input");
                break;
        }
        System.out.println("want to process more? y/n");
        String x=in.next();
        char ch=x.charAt(0); 
        if( ch=='n')
            y=false;
    }
    while(y!=false);
   
    }
    
    //*************************************************************************************staticpublicvoid insert(){
       
       
       Scanner in=new Scanner(System.in);
        
        boolean y=true;
       do{
            System.out.println("enter no to add in list");
            int num=in.nextInt();
            if(num==0)
                System.out.println("0 is not alowed");
            else
                l.add(num);
            System.out.println("want to insert more?y/n");
            String x=in.next();
            char ch=x.charAt(0);
            if( ch=='n')
                y=false;
         }
       while(y!=false); 
   }
   
  //*************************************************************************************8 staticpublicvoid print(){
       
       
       if(l.isEmpty())
           System.out.println("list is empty ");
       else
            System.out.println("members of lists are:");
            for(int i=0 ; i<l.size();i++)
                System.out.println("index "+i+" : "+ l.get(i)+" ");
      
   }
   
   //********************************************************************************staticpublicvoid search(){
       
       //List<Integer> I=new ArrayList<Integer>();
       System.out.println("enter no to search");
       Scanner in=new Scanner(System.in);
       
       int num=in.nextInt();
       for(int i=0;i<l.size();i++){
           if(l.get(i)==num)
                System.out.println(num+" is  PRESENT at Index "+i);
       }
         
       if(l.lastIndexOf(num)==-1)
               System.out.println(num+" is NOT PRESENT");
   }

   //*****************************************************************************8staticpublicvoid edit(){
       
       
       Scanner in=new Scanner(System.in);
       int num1,num2;
       System.out.println("enter no you want to add");
       num2=in.nextInt();
       if(num2==0)
             System.out.println("0 is not alowed");
       else{
             try{
                System.out.println("enter position you want to edit");
                num1=in.nextInt();
                l.set(num1, num2);
             }catch(IndexOutOfBoundsException e){
                    System.err.println("caught IndexOutOfBoundsException: specified position is empty "+e.getMessage());
               }
       
          }
   }
   
   
    //************************************************************************************************8staticpublicvoid delete(){
        
        
        Scanner in=new Scanner(System.in);
        System.out.println("enter position you want to delete");
        int num=in.nextInt();
        l.remove(num);
        
    }
    
    
     //***********************************************************************88staticpublicvoid total(){
        
        
        System.out.println("total no. in list are "+ l.size());
    }
    
    //******************************************************************************staticpublicvoid toexit(){
        
        System.exit(0);
    }
}

  
Share: 


Didn't find what you were looking for? Find more on Add,delete,Update in LISTS Or get search suggestion and latest updates.

Falak Khan
Falak Khan author of Add,delete,Update in LISTS is from Pakistan.
 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!