How do you do the following? a) Create an ArrayList for storing double values? b
ID: 3824728 • Letter: H
Question
How do you do the following? a) Create an ArrayList for storing double values? b) Append an object to a list? c) Insert an object at the beginning of a list? D) Find the number of objects in a list? e) Remove a given object from a list? f) Remove the last object from the list? g) Check whether a given object is in a list? h) Retrieve an object at a specified index from a list? Suppose an ArrayList contains {"Dallas", "Dallas", "Houston", "Dallas"}. What is the list after invoking list.remove("Dallas") one time? Provide the code to remove all elements with value "Dallas" Identify the errors in the following code: ArrayList list = new ArrayList(); list. add ("Denver"); list. add ("Austin") list. add (new java.util. Date()); String city = list.get (0); list.set (3, "Dallas") System.out. println (list.get(3));Explanation / Answer
1. a. ArrayList<Double> al = new ArrayList();
b. List l = new List();
l.add(new Object());
c. List l = new ArrayList();
l.add(0, new Object());
d. l.size() method returns the size of the list
e. l.remove(Object O) method removes the provided object
f. l.remove(l.size()-1) method removes the last object in the list
g. l.contains(Object o) method returns boolean depends on whether the object is present or not
h.l.get(index) method retrieves the Object at the given index
2. list.remove(Object o) method removes the first occurance of the object in a list. SO, after removing the first one , list will be Dallas, Houston, Dallas
3. The given arraylist is of String data type. But, we are adding date here, which will causes error.
list.set and list. get methods will not work, as we are retrieving the element at index 3 which is not present here. and which is out of bounds
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.