Ex.
13 Program implementing ArrayList
Source Code
import java.util.ArrayList;
import java.util.Scanner;
public class StudentList
{
public static void main(String[] args)
{
ArrayList<String> students = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
// Adding students to the ArrayList
System.out.println("Enter student names (type 'stop' to finish):");
while (true)
{
System.out.print("Enter student name: ");
String name = scanner.nextLine();
if (name.equalsIgnoreCase("stop"))
break;
// Add student to the list
students.add(name);
}
// Display the list of students
System.out.println("\nList of students:");
for (String st : students)
System.out.println(st);
// Remove a student by name
System.out.print("\nEnter the name of the student to remove: ");
String studentToRemove = scanner.nextLine();
if (students.remove(studentToRemove))
System.out.println(studentToRemove + " has been removed.");
else
System.out.println(studentToRemove + " was not found in the list.");
// Display the updated list of students
System.out.println("\nUpdated list of students:");
for (String st : students)
System.out.println(student);
scanner.close();
}
}