Question

JAVA Create an ArrayList called list of type Character 2. Add the following five elements |...

JAVA

Create an ArrayList called list of type Character 2. Add the following five elements | < \ / > (4 points) 3. Print the elements of the list, with a label (3 points) 4. Delete the last element of the list (4 points) 5. Use the method size to determine how many elements there are in the list (no hard-coded numbers). Print the number of elements. (3 points) 6. Add a plus sign ( + ) to the list (4 points) 7. Add a dash ( - ) right after the first element (4 points) 8. Print the updated list (3 points) Important: Make sure that the code still works when I initialize the ArrayList with a different number of elements. Output: list: [|, <, \, /, >] number of elements in list: 4 list: [|, -, <, \, /, +]

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Output

Code with comments

import java.util.ArrayList;

public class ArrayListJava{

   public static void main(String[] args) {

       ArrayList<Character>list=new ArrayList<>();   //1) creating new list

       list.add('|');       //2)   add character to list

       list.add('<');

       list.add('\\');      // '\' is invalid symbol

       list.add('/');

       list.add('>');

       System.out.print("List: [ ");        //3)   print list

       for (int i=0;i<list.size();i++) {

            System.out.print(list.get(i));

            if(i!=list.size()-1)

               System.out.print(",");   //dont print comma after last element

       }

       System.out.println(" ]");

       list.remove(list.size()-1);      //4)remove last element

       System.out.println("number of elements in list: "+list.size());      //5) size of list

       list.add('+');   //6) add character

       list.add(1, '-');    //7) add character at position 1

       System.out.print("List: [ ");    //8) print list

       for (int i=0;i<list.size();i++) {

            System.out.print(list.get(i));

            if(i!=list.size()-1)

               System.out.print(",");

       }

       System.out.println(" ]");

       

   }

}

For indentation Purpose

Add a comment
Answer #2

answered by: Mayre Yıldırım
Add a comment
Know the answer?
Add Answer to:
JAVA Create an ArrayList called list of type Character 2. Add the following five elements |...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 3. Create a program named TestArrayList, and declare an ArrayList with an initial capacity of 10....

    3. Create a program named TestArrayList, and declare an ArrayList with an initial capacity of 10. Insert one copy of the string "Python", five copies of the string "Java" followed by four copies of the string "C++" so that your vector is now filled to сараcity. Add a class method named printArrayList to print out all the elements stored within an ArrayList on separate lines. Test your method to be sure it works a) Add a class method named delete...

  • iii.print out the arraylist iv.reverse all the elements v.print out this arraylist vi.make a clone of...

    iii.print out the arraylist iv.reverse all the elements v.print out this arraylist vi.make a clone of the arraylist vii.remove all the elements at any odd index of the original arraylist (not the cloned one) viii.print out the original arraylist ix.reverse the cloned arraylist x.print out the cloned arraylist (this arraylist should still contain the original sequence of elements in order) xi.merge the cloned arraylist to the original arraylist (please think about what happens and draw a diagram for yourself to...

  • JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class....

    JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class. Print out the results after testing each of the methods in both of the classes and solving a simple problem with them. Task 1 – ArrayList Class Create an ArrayList class. This is a class that uses an internal array, but manipulates the array so that the array can be dynamically changed. This class should contain a default and overloaded constructor, where the default...

  • Assume we already have an ArrayList of Integers called list. Write code that will add 1...

    Assume we already have an ArrayList of Integers called list. Write code that will add 1 to each element of the list. For example, if the list originally contained 2, 4,-3, then it should contain 3, 5, -2 after the lines of code execute. Just write the necessary lines of code (not a whole method or program). Assume we already created the ArrayList and filled it with Integers.

  • Create a java program that reads an input file named Friends.txt containing a list 4 names...

    Create a java program that reads an input file named Friends.txt containing a list 4 names (create this file using Notepad) and builds an ArrayList with them. Use sout<tab> to display a menu asking the user what they want to do:   1. Add a new name   2. Change a name 3. Delete a name 4. Print the list   or 5. Quit    When the user selects Quit your app creates a new friends.txt output file. Use methods: main( ) shouldn’t do...

  • java Create the following classes: DatabaseType: an interface that contains one method 1. Comparator getComparatorByTrait(String trait)...

    java Create the following classes: DatabaseType: an interface that contains one method 1. Comparator getComparatorByTrait(String trait) where Comparator is an interface in java.util. Database: a class that limits the types it can store to DatabaseTypes. The database will store the data in nodes, just like a linked list. The database will also let the user create an index for the database. An index is a sorted array (or in our case, a sorted ArrayList) of the data so that searches...

  • *Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods...

    *Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods that has the following generic methods: (1) Write the following method that returns a new ArrayList. The new list contains the nonduplicate (i.e., distinct) elements from the original list. public static ArrayList removeDuplicates(ArrayList list) (2) Write the following method that shuffles an ArrayList. It should do this specifically by swapping two indexes determined by the use of the random class (use Random rand =...

  • can you solve it in java please Create the following: 1. Class Invoice ( the node...

    can you solve it in java please Create the following: 1. Class Invoice ( the node ) that includes three instance variables:     int No; // the Invoice No             String CustName; // the Customer name             int Amount; // the Invoice Amount Invoice next; // points to the next Invoice Default and overloaded constructors 2. Class Shop that includes three instance variables: Invoice head; Invoice Tail; Your class should have the following: • A method that initializes the instance variables....

  • Create a Java code that includes all the methods from the Lecture slides following the ADTs...

    Create a Java code that includes all the methods from the Lecture slides following the ADTs LECTURE SLIDE Collect/finish the Java code (interface and the complete working classes) from lecture slides for the following ADTS 2) ArrayList ADT that uses a linked list internally (call it LArrayList) Make sure you keep the same method names as in the slides (automatic testing will be performed)! For each method you develop, add comments and estimate the big-O running time of its algorithm....

  • Write a Python program (remove_first_char.py) that removes the first character from each item in a list...

    Write a Python program (remove_first_char.py) that removes the first character from each item in a list of words. Sometimes, we come across an issue in which we require to delete the first character from each string, that we might have added by mistake and we need to extend this to the whole list. Having shorthands (like this program) to perform this particular job is always a plus. Your program should contain two functions: (1) remove_first(list): This function takes in a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT