Java List Functions

This is a simple java program about list for beginners. Array list allows programmers to performs different operations like add, delete and modify the list.

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

Share on Google Plus

About Asad

Asad Niazi is Software Engineer , Programmer, Web Developer and a young mentor of BloggersTown and PProgramming. Asad Love to writes about Technology, Programming, Blogging and make money online.

1 comments:

  1. This is exactly the kind of information that makes me love the internet just a little bit more every day.I was perplexed for a while, great post.You have really helped me thank you very much :)

    ReplyDelete