Question: Create an ArrayList of Strings called avengers add Captian America, Hulk, Iron Man, Thor in that order. add Black Panther at position 1 Replace the

Create an ArrayList of Strings called avengers

add "Captian America", "Hulk", "Iron Man", "Thor" in that order.

add "Black Panther" at position 1

Replace the element at index 2 with "Black Widow" Use the set method

Replace the next to the last element with "Hawkeye". Use the set method. Do this in a manner that would replace the next to the last element, no matter the size of the array list. Think about how to find the index of the last element in an array list, then find the index of the next to the last element and use that in the set method. Don't just use 3 in the set method

remove "Thor" Remove the object by name. Do not use the index. We want to remove Thor no matter where it is in the ArrayList. (See the note below)

get and print the 0th element followed by "***"

Print the elements on one line in the [xxx, yyy, ...] format. (See the note below)

Print the entire array list one element to a line (Use the enhanced for loop)

Note: There is a version of remove method that will remove a specific object. Use that. Here is the code to remove target from the ArrayList called stuff

stuff.remove(target);

Note: To print an ArrayList in the [xxx, yyy, ...] format, use its toString method like this

System.out.println(stuff.toString());

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!