To add an element at a specific location in an ArrayList, use: listName.add(index, valueToBeAdded) where index is within the bounds 0 to listName.size()
Q1: Put the following code statements in the correct order for a method that takes an array list of doubles and subtracts the value of the second parameter from all the values in the list.
Q2: Put the following code statements in the correct order for a method that takes all strings that have a length longer than minLength and puts them in a new ArrayList that is returned.
Q3: Put the following code in order to create a program that will declare and instantiate an ArrayList of 20 random Integer values between 1 and 100 and then calculate and print the average of the first 10 numbers.
import java.util.*;
public class main{
public static void main (String[] args) {
---
Random r = new Random();
ArrayList<Integer> arr = new ArrayList<Integer>();
int sum = 0;
---
for (int i = 0; i < 20; i++) {
arr.add(r.nextInt(100) + 1);
System.out.println("arr.get("+i+") is " + arr.get(i));
}
---
for (int num: arr)
---
sum += num;
---
System.out.println("Avg of first 10 is " + sum/10.0);
---
}
}
public static int count (ArrayList<Integer> arr, int target) {
---
int num = 0;
---
for (int i = 0; i < arr.size(); i++) {
---
if (arr.get(i) == target)
---
num++;
---
}
---
return num;
}