Trace the following program step by step, to find the program modification of th
ID: 3712868 • Letter: T
Question
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<String> names = new java.util.ArrayList<String>();
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);
}
}
Explanation / Answer
Basically what the program is doing is checking whether the new name that user is entering exists in arraylist or not and if the name exists it is removed from list and if does not exists, it is added.
When user enters the name 'ahmed', so the arraylist names already has names 'Eman', 'Rwan', 'Ahmed', 'Kaled'.
Variable x holds the entered value, hence x='ahmed', and variable removed_flag is set false. Now the loop starts, it works from 0 to size-1 of the arrayList names that is (4-1) 3. When the loop starts i=0, gets into loop , checks whether i is lesser that size of array list which it is , so it gets the name at the index i from arraylist names and stores in pName, hence pName = 'Eman' , now pName is compared with x , the function equalsIgnoreCase() checks whether the two strngs are actually equal ignoring in what case they are. So pName had 'Eman' and x has 'ahmed' which are not equal so it does not get into if bracket and goes back to increasing i.
Now value of i=1, gets in loop x='ahmed' now value of pName='Rwan', similarly to previous description, pName is compared to x and result is false. Hence doesnot get into if bracket and value of i is increased.
Now value of i=2, gets in loop x='ahmed' now value of pName='Ahmed', they are compared and condition is resulted to true hence they get into if bracket. through this names.remove(i) line is excueted which removes the name 'Ahmed' from arraylist and sets removed_flag to true.
Now i=3, gets in loop x='ahmed, pName ='Kaled', fails if condition hence does not get into if bracket and value of i is increased.
Now i=4, and it fails the entering condition of for loop therefore loop ends.
Now since removed_flag is set to true then it doesnot get into last if condition and lastly Prints :
Updated names : Eman Rwan Kaled.
Now when user enters 'Salem',
arraylist names are Eman, Rwan, Ahmed, Kaled. variable x has salem so again it starts. Size of arraylist is 4 so for loop will run from 0 to 3. Following the same description of the program that I did for x='Ahmed', here the name entered by User is Salem and it will not be fond in arraylist so it will never be removed as it was not in the arraylist and removed_flag will remain false. No since remove_flag is false, it will get into last if condition and names.add(x) will be executed which will add x in the arraylist names.
Then lastly following is printed :
Updated names : Eman, Rwan, Ahmed, Kaled, Salem.
Hope this helps you. Feel free to reach out in case of any doubts. Thank you.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.