In: Computer Science
You can follow the discussion of the java.util package's ArrayList class from the textbook or the lecture, but I'm hoping you will be prompted to review the Java online documentation at:
https://docs.oracle.com/en/java/javase/11/docs/api/allclasses.html (Links to an external site.)
You can do this exercise mentally, but it might be better if you
write a Java program to implement an ArrayList instance of type
String by performing the following operations:
SOLUTION-
I have solve the problem in Java code with
comments and screenshot for easy understanding :)
CODE-
//java code
// for ArrayList class
import java.util.ArrayList;
public class Main {
/* main method */
public static void main(String args[]) {
/* Creating Array
List of String type */
ArrayList<String>
lst = new ArrayList<>();
/* adding elements
*/
lst.add("a");
lst.add("day");
/* adding element at
index 1 */
lst.add(1, "bad");
/* adding elements
*/
lst.add("good");
lst.add("day");
/* removing element
"bad" */
lst.remove("bad");
/* adding "any" at
index 0 */
lst.add(0, "any");
/* setting "have" at
index 0, previous element at 0 gets replaced */
lst.set(0, "have");
/* removing element
at index 2 */
lst.remove(2);
String msg = "";
/* Listing the message
from array list */
for (String element :
lst) {
msg += element + " ";
}
//printing the final
message
System.out.println("Final-Message: " + msg);
}
}
SCREENSHOT-
IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I WILL
SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK
YOU!!!!!!!!----------