Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Trace the following program step by step, to find the program modification of the names array list when user enter ahmed. Write the output when

Trace the following program step by step, to find the program modification of the names array list when user enter ahmed. Write the output when the user enters ahemd and when enters salem.

import java.util.Scanner;

public class Student_ArrayList {

public static void main(String[] args) {

java.util.ArrayList names = new java.util.ArrayList();

names.add("Eman");

names.add("Rwan");

names.add("Ahmed");

names.add("Kaled");

System.out.print("Names: ");

System.out.println(names);

Scanner in = new Scanner(System.in);

System.out.print("Enter a name: ");

String x = in.next();

boolean removed_flag= false;

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

{

if (i < names.size()) {

String pName = names.get(i);

if (pName.equalsIgnoreCase(x)) {

names.remove(i);

removed_flag= true;

}

}

}

if (removed_flag==false) {

names.add(x);

}

System.out.print("Updated names: ");

System.out.println(names);

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Donald A. Carpenter Fred R. McFadden

1st Edition

8178088045, 978-8178088044

More Books

Students also viewed these Databases questions