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...
Grocery Store using Java Arraylist Requirements: 1. Need Product Class and Stock Class 2. Use arraylist...
Grocery Store using Java Arraylist Requirements: 1. Need Product Class and Stock Class 2. Use arraylist to store/ add product in stock 3. Product must have name, code number and quantity in stock 4. Print list of the products and their stock quantity 5. Search products based on their name 6. Remove product based on their code number 7. Sell product and deduct quantity from stock 8. Stock Class contains methods of search product, add product and check quantity of...
In Lecture 8(ArrayList, Generics) , we discussed about creating a MyStack<E> class using the ArrayList<E> class...
In Lecture 8(ArrayList, Generics) , we discussed about creating a MyStack<E> class using the ArrayList<E> class that has the stack methods ( isEmpty(), getSize(), peek(), pop(), push(Object o), and toString( ) ) as shown in the following: public class MyStack<E> { private ArrayList<E> list = new ArrayList<>(); public int getSize() {     return list.size(); } public E peek() {     return list.get(getSize() - 1); } public void push(E o) {     list.add(o); } public E pop() {     E o = list.get(getSize()...
Grocery Store using Java Arraylist Requirements: 1. Need Product Class and Stock Class 2. Use arraylist...
Grocery Store using Java Arraylist Requirements: 1. Need Product Class and Stock Class 2. Use arraylist to store/ add product in stock 3. Product must have name, code number and quantity in stock 4. Print list of the products and their stock quantity 5. Search products based on their name 6. Remove product based on their code number 7. Sell product and deduct quantity from stock 8. Stock Class contains methods of search product, add product and check quantity of...
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)  ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT