
API documentation link ------> https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/ArrayList.html
PREVIOUS CODE -
import java.util.ArrayList;
public class BasicArrayList {
// main method
public static void main(String[] args) {
// create an arraylist of type Integer
ArrayList<Integer> basic = new ArrayList<Integer>();
// using loop add elements
for(int i=0;i<4;i++){
basic.add(i+1);
}
// using loop set elements
for(int i=0;i<basic.size();i++){
basic.set(i,(basic.get(i)*5));
}
// using loop print elements
for(int i=0;i<basic.size();i++){
System.out.println(basic.get(i));
}
}
}
Q1)
a)Consider how to remove elements from the ArrayList. Give two different lines of code that would both remove 5 from the ArrayList.
b)What do you think the contents of the ArrayList will look like after the 5 has been removed? (Sketch or list)
c)Now update the BasicArrayList program to remove the 5 (index 1) from the ArrayList. Add this step before the last loop that prints out the contents fo the ArrayList. Examine the output and compare it to your answer in the previous question? Were there any surprises (or bugs) in your code?
d) Now go back and examine the API documentation for the ArrayList class (Links to an external site.).
i) Give an example line of code, calling an ArrayList method that would help you search the ArrayList for a specific element.
ii) What will this method return if the element is not found?
e)Referring back to the API documentation for the ArrayList class (Links to an external site.),
i) Give an example line of code that calls an ArrayList method that would remove all elements from the ArrayList.
ii) Give an example line of code that calls an ArrayList method that would check if the ArrayList has no elements in it.
f)
Update your BasicArrayList program by adding the following:
- before the last for-loop, add a statement to set the element in position 0 to 1
- after the last for-loop add code to s
- search the ArrayList for the number 5 and display the index if found
- search the ArrayList for the number 15 and display the index if found
- check if the array is empty and print an appropriate message (e.g., "ArrayList is empty/not empty")
.
Recalling from the previous activity, the contents of the ArrayList after step 5 on the last question (where we converted the BasicArray program to BasicArrayList and a new element is added to position 0), are as follows:
| 0 | 5 | 10 | 15 | 20 |
|----|----|----|----|----|
In this table, each cell represents an element in the ArrayList at positions 0 through 4.
For further reading and more advanced methods, refer to the ArrayList API documentation linked above.](https://content.bartleby.com/qna-images/question/6cf78908-a1f9-4330-9ece-f4021df10613/2b96e1d7-f756-4c43-95c7-2b75f5a2514e/0ft8c0f_thumbnail.png)

Trending nowThis is a popular solution!
Step by stepSolved in 4 steps with 3 images

- 44 // create a loop that reads in the next int from the scanner to the intList 45 46 47 // call the ReverseArray function 48 49 50 System.out.print("Your output is: "); 51 // print the output from ReverseArray 52 ww 53 54 55 }arrow_forwardpackage edu.umsl.iterator;import java.util.ArrayList;import java.util.Arrays;import java.util.Collection;import java.util.Iterator;public class Main {public static void main(String[] args) {String[] cities = {"New York", "Atlanta", "Dallas", "Madison"};Collection<String> stringCollection = new ArrayList<>(Arrays.asList(cities));Iterator<String> iterator = stringCollection.iterator();while (iterator.hasNext()) {System.out.println(/* Fill in here */);}}} Rewrite the while loop to print out the collection using an iterator. Group of answer choices iterator.toString() iterator.getClass(java.lang.String) iterator.remove() iterator.next()arrow_forwardimport java.util.HashSet; import java.util.Set; // Define a class named LinearSearchSet public class LinearSearchSet { // Define a method named linearSearch that takes in a Set and an integer target // as parameters public static boolean linearSearch(Set<Integer> set, int target) { // Iterate over all elements in the Set for () { // Check if the current value is equal to the target if () { // If so, return true } } // If the target was not found, return false } // Define the main method public static void main(String[] args) { // Create a HashSet of integers and populate integer values Set<Integer> numbers = new HashSet<>(); // Define the target to search for numbers.add(3); numbers.add(6); numbers.add(2); numbers.add(9); numbers.add(11); // Call the linearSearch method with the set…arrow_forward
- import java.util.*; public class Main{ public static void main(String[] args) { Main m = new Main(); m.go(); } private void go() { List<Stadium> parks = new ArrayList<Stadium>(); parks.add(new Stadium("PNC Park", "Pittsburgh", 38362, true)); parks.add(new Stadium("Dodgers Stadium", "Los Angeles", 56000, true)); parks.add(new Stadium("Citizens Bank Park", "Philadelphia", 43035, false)); parks.add(new Stadium("Coors Field", "Denver", 50398, true)); parks.add(new Stadium("Yankee Stadium", "New York", 54251, false)); parks.add(new Stadium("AT&T Park", "San Francisco", 41915, true)); parks.add(new Stadium("Citi Field", "New York", 41922, false)); parks.add(new Stadium("Angels Stadium", "Los Angeles", 45050, true)); Collections.sort(parks, Stadium.ByKidZoneCityName.getInstance()); for (Stadium s : parks) System.out.println(s); }}…arrow_forward*JAVA* complete method Delete the largest valueremoveMax(); Delete the smallest valueremoveMin(); class BinarHeap<T> { int root; static int[] arr; static int size; public BinarHeap() { arr = new int[50]; size = 0; } public void insert(int val) { arr[++size] = val; bubbleUP(size); } public void bubbleUP(int i) { int parent = (i) / 2; while (i > 1 && arr[parent] > arr[i]) { int temp = arr[parent]; arr[parent] = arr[i]; arr[i] = temp; i = parent; } } public int retMin() { return arr[1]; } public void removeMin() { } public void removeMax() { } public void print() { for (int i = 0; i <= size; i++) { System.out.print( arr[i] + " "); } }} public class BinarH { public static void main(String[] args) { BinarHeap Heap1 = new BinarHeap();…arrow_forwardJava Foundations Consider the following Java method to reverse an array, i.e. rearrange its elements to the opposite order. An array containing {1, 3, 5} would contain {5, 3, 1} after being reversed. public static void arrayReverse(int[] array) { int le, ri; for (le = 0, ri = array.length - 1; le < array.length / 2; le++, ri--) { int temp = array[le]; array[le] = array[ri]; array[ri] = temp; } } Write the worst-case runtime efficiency of arrayReverse in BigOh notation.arrow_forward
- java Create a method that: is called timesTwo returns an ArrayList of Integers takes in a single parameter - an ArrayList of Integers called nums This method should take the ArrayList parameter and multiply every value by two. public static void main(String[] args) { Scanner in = new Scanner(System.in); int size = in.nextInt(); ArrayList<Integer> list = new ArrayList<>(); for(int i=0; i < size; i++) { list.add(in.nextInt()); } System.out.println(timesTwo(list)); } }arrow_forwardCode debug JAVA import java.util.ArrayList;public class ArrayList {public static ArrayList<Integer> reverse(ArrayList<Integer> list) {for (int i = 0; i < list.size(); i++) {System.out.println(list);}public static ArrayList<Integer> getReverse(ArrayList<Integer> list){for (int index = 0; index < list.size() / 2; index++) {int temp = list.get(index);list.set(index, list.get(list.size() - index - 1));//swaplist.set(list.size() - index - 1, temp); //swap}return list;}}public static void main(String[] args) {ArrayList<Integer> list = new ArrayList<>();list.add(1);list.add(2);list.add(3);System.out.println("Original elements : " + list);System.out.println("Reversed elements : " + getReverse(list));}}arrow_forwardpublic class arrayOutput ( public static void main (String [] args) { final int NUM ELEMENTS = 3; int[] userVals = new int [NUM_ELEMENTS]; int i; } Type the program's output userVals [0] = 2; userVals [1] = 6; userVals [2] = 8; for (i = userVals.length - 1; i >= 0; −−1) { System.out.println(userVals [1]); } C.C. ? ? ??arrow_forward
- Need help with menu loop pleasearrow_forwardJava language Use arrays in creating your class. Stack Interface (LIFO) void push(Object) Object pop() String toString() boolean isEmpty() boolean equals(Object) getIndexOf get remove private static void arrayListTests() { System.out.println("ArrayList Tests"); // todo: make more tests here ArrayList a = new ArrayList(); System.out.println("Check empty array isEmpty:" + a.isEmpty()); a.insert('B', 0); a.insert('a', 0); a.insert('t', 1); System.out.println("Check non-empty array isEmpty:" + a.isEmpty()); System.out.println(a.toString()); while (a.isEmpty() == false) { System.out.println(a.remove(0)); } // Fill over initial capacity and check that it grows for (int i = 0; i < 110; i++) { a.append(new Integer(i)); } System.out.println("Size of array after 110 adds: "+ a.size()); System.out.println("Value of last element: "+ a.get(a.size()-1)); System.out.println("Insert past end of list"); a.insert('z', 200); System.out.println("Insert negative index"); a.insert('z', -3);…arrow_forwardExercise 2: Consider the following class: public class Sequence { private ArrayList values; public Sequence() { values = new ArrayList(); } public void add(int n) { values.add(n); } public String toString() { return values.toString(); } } Add a method public Sequence merge(Sequence other) that merges two sequences, alternating ele- ments from both sequences. If one sequence is shorter than the other, then alternate as long as you can and then append the remaining elements from the longer sequence. For example, if a is 1 4 9 16 and b is 9 7 4 9 11 then a.merge(b) returns the sequence 1 9 4 7 9 4 16 9 11 without modifying a or b.arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





