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:...
import java.util.*; class Main { static ArrayList<String> list; public static List<String> createList(ArrayList<String> arrayList) { list =...
import java.util.*; class Main { static ArrayList<String> list; public static List<String> createList(ArrayList<String> arrayList) { list = arrayList; return list; } public static void printList(ArrayList<String> arrayList) { System.out.println("Printing in 4 ways\n"); // 1 System.out.println(arrayList); //2 for(String s:arrayList) System.out.print(s+" "); System.out.println(); //3 System.out.println(Arrays.deepToString(list.toArray())); //4 for(int i=0;i<arrayList.size();i++) System.out.print(arrayList.get(i)+" "); System.out.println(); } public static void filterList(ArrayList<String> arrayList) { System.out.println("Filtered in 2 ways\n"); ArrayList<String> copyArrayList = arrayList; //1 for(int i=0;i<arrayList.size();i++) { if(arrayList.get(i).contains("chuck")) { arrayList.remove(i); i--; } } System.out.println(arrayList); //2 copyArrayList.removeIf(str -> str.contains("chunk")); System.out.println(copyArrayList); }   ...
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...
Add code to the Account class and create a new class called BalanceComparator. import java.util.*; public...
Add code to the Account class and create a new class called BalanceComparator. import java.util.*; public final class Account implements Comparable {     private String firstName;     private String lastName;     private int accountNumber;     private double balance;     private boolean isNewAccount;     public Account(             String firstName,             String lastName,             int accountNumber,             double balance,             boolean isNewAccount     ) {         this.firstName = firstName;         this.lastName = lastName;         this.accountNumber = accountNumber;         this.balance = balance;         this.isNewAccount = isNewAccount;     }     /**      * TO DO: override equals      */     @Override     public boolean equals(Object other) {...
***Given a class called Student and a class called Course that contains an ArrayList of Student....
***Given a class called Student and a class called Course that contains an ArrayList of Student. Write a method called dropStudent() as described below. Refer to Student.java below to learn what methods are available.*** Course.java import java.util.*; import java.io.*; /****************************************************** * A list of students in a course *****************************************************/ public class Course{ /** collection of Students */ private ArrayList<Student> roster; /***************************************************** Constructor for objects of class Course *****************************************************/ public Course(){ roster = new ArrayList<Student>(); } /***************************************************** Remove student with the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT