Question: Java Start with an empty ArrayList of integers (new ArrayList ()). Then, we can perform the following commands: append elem : Appends integer elem at
Java
Start with an empty ArrayList of integers (new ArrayList
append elem: Appends integer elem at the end of the ArrayList.
insert index elem: Inserts integer elem at position index in the ArrayList.
remove elem: Removes the first occurrence of integer elem from the ArrayList.
set index elem: Replaces the integer at position index in the ArrayList with integer elem.
pop: Remove the last integer from the ArrayList.
print: Prints the ArrayList.
reverse: Reverses the integers in the ArrayList.
After creating the empty ArrayList of integers, read in the value of n followed by n lines of commands where each command will be of the 7 types listed above. Iterate through each command in order and perform the corresponding operation on the ArrayList.
Input Format
The first line contains an integer, n, denoting the number of commands. Each line i of the subsequent lines contains one of the commands described above.
Constraints
The elements added to the ArrayList must be integers.
Output Format
For each command of type print, print the list on a new line.
Sample Input 0
11 insert 0 5 insert 1 10 insert 0 6 print remove 6 append 9 append 1 print pop reverse print
Sample Output 0
[6, 5, 10] [5, 10, 9, 1] [9, 10, 5]
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
