In: Computer Science
For each of the following write the line(s) of code that:
Declares and initializes (creates) an ArrayList that holds String objects
Adds to your ArrayList the words "Too" and "Fun"
Verifies that your ArrayList now contains 2 elements
Sets the second String in the ArrayList to "No"
Verifies that your ArrayList still contains exactly 2 elements
Prints the contents of the ArrayList to the screen in the following format:
<element>, <element>, . . . , <element>
The programming language used is python 3.8
1)Declaring the ArrayLIst:
The code in python : ArrayList = []
2)Adding string values to ArrayList:
The code in python:
ArrayList.append("Too")
ArrayList.append("Fun")
3)Verifying ArrayList has 2 elements:
This is done by printing the values in ArrayList.The python code is:
for value in ArrayList:
print(value,end=" ")
4)Setting the second string in ArrayList to "No"
The index of second string in ArrayList will be 1.
So the python code is:
ArrayList[1]="No"
5)Verifying ArrayList has 2 elements:
This is done by printing the values in ArrayList.The python code is:
for value in ArrayList:
print(value,end=" ")
6)Printing of the contents in ArrayList can be done using the code:
for value in ArrayList:
print(value,end=" ")
The python code snippet is provided below:
ArrayList=[]
ArrayList.append("Too")
ArrayList.append("Fun")
for value in ArrayList:
print(value,end=" ") #To verify there are 2 contents in array list
ArrayList[1]="No"
print("")
for value in ArrayList:
print(value,end=" ") #To verify there are 2 contents in array list
print(" ")
#To print the contents
for value in ArrayList:
print(value,end=" ")
The screenshot of the code and output generated is attached for your reference:
Hope it helps!!!