Showing posts with label Java List Functions. Show all posts
Showing posts with label Java List Functions. Show all posts

Saturday, 11 October 2014

Java List Functions

Structure of the Problem Requirements 

In this problem we will extend out last problem in which we have done just two function of list. One is insert and other is Search function. Now we add the another function which is Delete(). In this function user can specify the index of number and according to that this function we remove the number which is saved on that index. So in this Problem first you insert number in the list and then play with the search and delete functions.Here is the source code of this problem which help you in better understanding .

SOURCE CODE



import java.util.*;

import java.util.Scanner;

public class ListFunction {

   

      static  List<Integer> integer_list = new ArrayList<Integer>();

      public static void main(String[] args) {

      Scanner input=new Scanner(System.in);

        boolean y=true;

        System.out.println("**** List in Java ******* ");

        do{  

         System.out.println(" 1 : Insert ");

         System.out.println(" 2 : Search ");

         System.out.println(" 3 : Delete ");

         System.out.println(" Choice : ");

         int choice=input.nextInt();

         switch(choice)

         {

            case 1:

                insert();

                break;

            case 2:

                search();

                break;

            case 3:

                delete();

                break;


            default:

                System.out.println("Invalid Input");

                break;

        }

        System.out.println("Want Again (y/n) ? ");

        String x=input.next();

        char ch=x.charAt(0);

        if( ch=='n')

            y=false;

    }

    while(y!=false);


    }

    public static void insert()

    {

           

       Scanner insert=new Scanner(System.in);

        

        boolean y=true;

       do{

            System.out.println("Enter Number : ");

            int num=insert.nextInt();

            if(num==0)

                System.out.println("Enter Number Greater Than Zero ! ");

            else

                integer_list.add(num);

            System.out.println("Insert Again (y/n) ?");

            String next=insert.next();

            char ch=next.charAt(0);

            if( ch=='n')

                y=false;

         }

       while(y!=false);

   }

     public static void search()

     {

       System.out.println("Enter Number for Search = ");

       Scanner search=new Scanner(System.in);

       int num=search.nextInt();

       for(int i=0;i<integer_list.size();i++){

           if(integer_list.get(i)==num)

                System.out.println(num+" Found in Index : "+i);

       }

       

       if(integer_list.lastIndexOf(num)==-1)

               System.out.println(num+" Not Present :( ");

     }


    public static void delete()
    {
        Scanner delete=new Scanner(System.in);
        System.out.println("Enter Index Number for Delete = ");
        int delete_num=delete.nextInt();
        integer_list.remove(delete_num);
    }  
}

OUTPUT OF THE PROGRAM

Java List
Java List functions