Q:

Define a collection “ArrayList” to store the students' names. Take 5 students’ names from the user and store them in the collection

0

Define a collection “ArrayList” to store the students' names.

Take 5 students’ names from the user and store them in the collection.

Add three student names to the collection and remove a student name. Insert a student name in second place in the collection.

Ask the user to enter a student name and search for it in the collection and prints the result as “Exist” or “Not Exist”.

Print the whole collection using the enhanced for loop.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

import java.util.*;
public class JavaApplication134 {
    public static void main(String[] args) {
    ArrayList <String>Students = new ArrayList<String>();
    Scanner input = new Scanner(System.in);
        for (int i = 0; i < 5; i++) {
            System.out.println("Enter Student name");
            Students.add(input.next());
        }
        Students.add("mohammed");
        Students.add("khaled");
        Students.add("anas");
        Students.remove(1);
        Students.add(1,"saad");
        
        System.out.println("enter a student name to search: ");
        String s = input.next() ;

        //searching element 
        boolean isFound = Students.contains(s);
        if (isFound == false)
            System.out.println("Element is not found in the list");
        else
            System.out.println("Element is found in the list");

        //printing the collection using enhanced for loop
        for(String stu:Students)
        {
            System.out.println(stu);
        }
    }
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now