JAVA ARRAYLIST

Write a program to declare arraylist, passing and returning arraylist and using arraylist functions.


import java.util.*;
class P20ArrayList
{
    public static void main()
    {
        ArrayList <String> fruits=new ArrayList<>();//declaration of array list
        System.out.println("  To add name of fruit press 1  ");
        System.out.println("  To access name of fruit press 2  ");
        System.out.println("  To change name of fruit press 3  ");
        System.out.println("  To remove name of fruit press 4  ");
        System.out.println("  To know the number of fruit press 5  ");
        System.out.println("  To sort fruit alphabetically press 6  ");
        System.out.println("  To remove all fruit press 7  ");
        System.out.println("  To view current list press 8  ");
        System.out.println("  To exit the program press 9 ");
        Scanner in = new Scanner(System.in);
        int a;
        do
        {   
            System.out.println(" \n INPUT YOUR CHOICE \n ");
            a = in.nextInt();
            switch(a)
            {
                case 1: fruits = addItems(fruits); break;
                case 2: accessItems(fruits); break;
                case 3: fruits = changeItems(fruits); break;
                case 4: fruits = removeItems(fruits); break;
                case 5: System.out.println("  number of fruits is "+fruits.size()); break;
                case 6: Collections.sort(fruits);
                System.out.println("  Fruits in alpphabetical order "+fruits); break;
                case 7: fruits.clear();
                System.out.println("  All fruits are removed "+fruits); break;
                case 8: System.out.println("  Current fruit list "+fruits); break;
            }
        }while(a!=9);
    }

    static ArrayList<String> addItems(ArrayList <String> fr)
    {
        Scanner in = new Scanner(System.in);
        System.out.println(" \n Enter name of fruits \n ");
        String name= in.next();
        fr.add(name);
        System.out.println(" \n Updated List "+fr);
        return fr;
    }

    static void accessItems(ArrayList <String> fr)
    {
        Scanner in = new Scanner(System.in);
        System.out.println(" \n Enter index of fruit \n ");
        int index= in.nextInt();
        System.out.println(" \n Fruit at given index is "+fr.get(index));
    }

    static ArrayList<String> changeItems(ArrayList <String> fr)
    {
        Scanner in = new Scanner(System.in);
        System.out.println(" \n Enter name of fruits \n ");
        String name= in.next();
        System.out.println(" \n Enter index at which you want to replace \n ");
        int index= in.nextInt();
        fr.set(index,name);
        System.out.println(" \n Updated List "+fr);
        return fr;
    }

    static ArrayList<String> removeItems(ArrayList <String> fr)
    {
        Scanner in = new Scanner(System.in);
        System.out.println(" \n Enter index at which you want to replace \n ");
        int index= in.nextInt();
        fr.remove(index);
        System.out.println(" \n Updated List "+fr);
        return fr;
    }
}


For any desired program, please comment below. We will try our best to make that program available.

Comments

Popular Posts