This is in java and please explain how you get to the final result and how expla
ID: 3788007 • Letter: T
Question
This is in java and please explain how you get to the final result and how explain everything works
Assume cityList is a type of ListInterface<String>. What will print after the following statements are executed:
System.out.println(cityList.isEmpty());
cityList.add("burbank");
cityList.add("carlsbad");
cityList.add("davis");
cityList.add("granada");
cityList.add("sacramento");
cityList.add(2, "hayward");
cityList.add("menlo park");
cityList.add(4, "napa");
System.out.println(cityList.isEmpty());
System.out.println(cityList.getLength());
System.out.println(cityList.getEntry(2));
System.out.println(cityList.remove(2));
System.out.println(cityList.getEntry(2));
System.out.println(cityList.remove(1));
System.out.println(cityList.remove(2));
System.out.println(cityList.remove(3));
System.out.println(cityList.getLength());
System.out.println(cityList.replace(2, "oakland"));
System.out.println(cityList.getEntry(2));
System.out.println(cityList.getEntry(3));
cityList.clear();
System.out.println(cityList.getLength());
Explanation / Answer
Here is the code explained as comments for each step:
System.out.println(cityList.isEmpty()); //This returns true.
cityList.add("burbank"); //This city will be added and therefore, the cityList is: burbank
cityList.add("carlsbad"); //This city will be added and therefore, the cityList is: burbank, carlsbad
cityList.add("davis"); //This city will be added and therefore, the cityList is: burbank, carlsbad, davis,
cityList.add("granada"); //This city will be added and therefore, the cityList is: burbank, carlsbad, davis, granada
cityList.add("sacramento"); //This city will be added and therefore, the cityList is: burbank, carlsbad, davis, granada, sacramento
cityList.add(2, "hayward"); //This city will be added at position 2, the cityList is: burbank, hayward, carlsbad, davis, granada, sacramento
cityList.add("menlo park"); //This city will be added and therefore, the cityList is: burbank, hayward, carlsbad, davis, granada, sacramento, menlo park
cityList.add(4, "napa"); //This city will be added at position 4, the cityList is: burbank, hayward, carlsbad, napa, davis, granada, sacramento, menlo park
System.out.println(cityList.isEmpty()); //Returns false, and will be printed.
System.out.println(cityList.getLength()); //Returns the length of the cityList, and will print 8.
System.out.println(cityList.getEntry(2)); //Returns the second entry of cityList, and will print hayward.
System.out.println(cityList.remove(2)); //Removes the second entry of cityList, and will print hayward.
//Now, the new cityList is: burbank, carlsbad, napa, davis, granada, sacramento, menlo park
System.out.println(cityList.getEntry(2)); //Returns the second entry of cityList, and will print carlsbad.
System.out.println(cityList.remove(1)); //Removes the first entry of cityList, and will print burbank.
//Now, the new cityList is: carlsbad, napa, davis, granada, sacramento, menlo park
System.out.println(cityList.remove(2)); //Removes the second entry of cityList, and will print napa.
//Now, the new cityList is: carlsbad, davis, granada, sacramento, menlo park
System.out.println(cityList.remove(3)); //Removes the third entry of cityList, and will print granada.
//Now, the cityList is: carlsbad, davis, sacramento, menlo park
System.out.println(cityList.getLength()); //Returns the length of the cityList, and will print 4.
System.out.println(cityList.replace(2, "oakland")); //Replaces the second entry with oakland.
//Now, the cityList is: carlsbad, oakland, sacramento, menlo park
System.out.println(cityList.getEntry(2)); //Returns the second entry of cityList, and will print oakland.
System.out.println(cityList.getEntry(3)); //Returns the third entry of cityList, and will print sacramento.
cityList.clear(); //Clears all the entries of the cityList.
System.out.println(cityList.getLength()); //Returns the length of the cityList, and will print 0.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.