Question

In: Computer Science

You can follow the discussion of the java.util package's ArrayList class from the textbook or the...

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:

  1. Instantiate an ArrayList containing type String members.
  2. add "a"
  3. add "day"
  4. add "bad" at index 1
  5. add "good"
  6. add "day"
  7. remove "bad"
  8. add "any" at index 0
  9. set "have" at index 0
  10. remove at index 2

Solutions

Expert Solution

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!!!!!!!!----------


Related Solutions

Convert this java code from hashmap into arraylist. import java.io.*; import java.util.*; public class Solution {...
Convert this java code from hashmap into arraylist. import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); HashMap labs = new HashMap(); while (true) { System.out.println("Choose operation : "); System.out.println("1. Create a Lab"); System.out.println("2. Modify a Lab"); System.out.println("3. Delete a Lab"); System.out.println("4. Assign a pc to a Lab"); System.out.println("5. Remove a pc from a Lab"); System.out.println("6. Quit"); int choice = sc.nextInt(); String name=sc.nextLine(); switch (choice) { case 1:...
Assignment For this discussion, you are asked to examine the structure of DNA from your textbook....
Assignment For this discussion, you are asked to examine the structure of DNA from your textbook. Using your knowledge or the material covered in Weeks 1 – 12, your general knowledge of organic chemistry I, your textbooks and the Internet to answer the following questions regarding the DNA molecule. Include the structure of DNA in your discussion. Label and give the name for the the major functional groups in the DNA molecule? Please be sure to include all functional groups...
1. For this discussion, you are asked to examine the structure of DNA from your textbook....
1. For this discussion, you are asked to examine the structure of DNA from your textbook. 2. Using your knowledge or the material covered in Weeks 1 – 12, your general knowledge of organic chemistry I, your textbooks and the Internet to answer the following questions regarding the DNA molecule. a. Label and give the name for the major functional groups in the DNA molecule? b. Functional groups are sites where chemical reactions take place. Using your knowledge of basic...
In this discussion, you will pick any set of data from the textbook or your own...
In this discussion, you will pick any set of data from the textbook or your own data. Conduct a confidence interval analysis. Explain in the discussion question: Your source of data The lower limit and upper limit How this information might be relevant to a decision maker. Attach the Excel file containing the data source (but be sure everything we need to know about your executive summary is in the body of the discussion forum, not the attachment).
You will generate a People class of object and load an ArrayList with person objects, then...
You will generate a People class of object and load an ArrayList with person objects, then report the contents of that ArrayList. To do so, you must perform the following: (30 pts.) A) (15/30 pts. (line break, 11 pt) ) - Create a class file “People.java” (which will generate People.class upon compile). People.java will have eight(8) methods to 1) read a .txt file ‘people.txt’ 2) generate: ▪ List of all students AND teachers ▪ List of all students OR teachers...
CONVERT CODE FROM JAVA TO C# PLEASE AND SHOW OUTPUT import java.util.*; public class TestPaperFolds {...
CONVERT CODE FROM JAVA TO C# PLEASE AND SHOW OUTPUT import java.util.*; public class TestPaperFolds {    public static void main(String[] args)    {        for(int i = 1; i <= 4; i++)               //loop for i = 1 to 4 folds        {            String fold_string = paperFold(i);   //call paperFold to get the String for i folds            System.out.println("For " + i + " folds we get: " + fold_string);        }    }    public static String paperFold(int numOfFolds)  ...
Develop the getSuggestions(ArrayList wordCountList) method as the base method of the class. Develop the countWords(ArrayList wordList)...
Develop the getSuggestions(ArrayList wordCountList) method as the base method of the class. Develop the countWords(ArrayList wordList) method to find the frequencies of all words in the text file. Develop the getWordList(File[] fileArray) method to get all words in the text file. Ignore the words that have 3 or less characters. Your customer wants you to develop a method that will find the sentences that contain a specific word. This is basically a word search, but your customer needs the list...
I need to implement incrementalInserstionSort im stuck on that part import java.util.*; /** * This class...
I need to implement incrementalInserstionSort im stuck on that part import java.util.*; /** * This class represents chains of linked nodes that * can be sorted by a Shell sort. * * @author Charles Hoot * @author Frank M. Carrano * Modified by atb * @author YOUR NAME * @version 9/29/2020 */ public class ChainSort> { private Node firstNode; // reference to first node public ChainSort() { this.firstNode = null; } public void display() { Node currentNode = this.firstNode; while...
class Company uses an ArrayList of class Employee to manage its employees. Your job is to...
class Company uses an ArrayList of class Employee to manage its employees. Your job is to create two classes , Company and Employee, with appropriate instance fields, constructors, and methods as been described in the set of questions that follows . A sample use case of the classes is shown below: public class CompanyTester{ public static void main(String[] args){ Company myCompany = new Company(); myCompany.add( new Employee("james","gasling")); myCompany.add( new Employee("bill","gate")); myCompany.add( new Employee("dennis","ritchie")); System.out.println(myCompany); } } The sample output of...
Discussion Topic: Use ethical dilemma (p. 178) What would you do? From the textbook. (Adams &...
Discussion Topic: Use ethical dilemma (p. 178) What would you do? From the textbook. (Adams & Galanes) ETHICAL DILEMMA What Would You Do? You and your group have been working on your panel presentation about the death penalty for several weeks, with one week to go before you are responsible for conducting the panel discussion in your class. This project represents a major portion of your grade in the small group class, and you are required to conclude your presentation...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT