How to Submit Please submit your answers to the lab instructor once you have com
ID: 3794200 • Letter: H
Question
How to Submit Please submit your answers to the lab instructor once you have completed. Failure to submit will result in a ZERO FOR THIS LAB. NO EXCEPTIONS. Write a program that prints out the lists alter the Following methods are called. Method minToFront: lakes an Arraynlist of integers as a parameter. The method moves the minimum value in the list to the front, otherwise preserving the order of the elements, f or example, if a variable called list stores the following values: [4, 7, 9, 2. 7, 7, 5. 3, 5, 1, 7, 8. 6, 7] and you make this call: minTofront.(list.); it should store the following values after the call: [1, . 4, 7, 9, 2, 7, 7, 5, 3, 5, 7, 8, 6, 7]. You may assume that the list stores at least one value. After the call print the list. Method filterRange: accepts an Arraylist of integers and two integer values min and max as parameters and removes all elements whose values are in the range min through max (inclusive) from the list. For example, if a variable called list stores the values: [1, 4, 7, 9. 2, 7. 7, 5, 3, 5, 7, 8, 6, 7] The call of filterRange (list., 5, 7) should remove all values between 5 and 7, therefore it should change the list to store [1, 4, 9, 2, 3, 8]. You may assume that the list is not null. After the call print the list. Method intersect: accepts two sorted array lists of integers as parameters and returns a new list that contains only the elements that are found in both lists. For example, if lists named list 1 and list2 initially store: [1, 4, 8, 9, 11, 15, 17, 28, 41, 59] [4, 7, 11, 17, 19, 20, 23, 28, 37, 59, 81] Then the call of intersect returns the list: [4, 11, 17, 28, 59] Print the list that is returned by this method.Explanation / Answer
Hi, please find my implementation.
Please let me know in case of any issue.
import java.util.ArrayList;
import java.util.Iterator;
public class ArrayListDemo {
public static void minToFront(ArrayList<Integer> list){
// finding the index of minimum element
int min_index = 0;
for(int i=1; i<list.size(); i++)
if(list.get(i) < list.get(min_index))
min_index = i;
// removing minimum elements and adding at front
int min = list.get(min_index);
list.remove(min_index);
list.add(0, min);
}
public static void filterRange(ArrayList<Integer> list, int min, int max){
Iterator<Integer> itr = list.iterator(); // remove all even numbers
while (itr.hasNext()) {
Integer number = itr.next();
if (number >= min && number <= max) {
list.remove(number);
}
}
}
public static ArrayList<Integer> intersect(ArrayList<Integer> list1, ArrayList<Integer> list2){
int i = 0;
int j = 0;
ArrayList<Integer> intersect = new ArrayList<>();
while(i<list1.size() && j < list2.size()){
if(list1.get(i) < list2.get(j))
i++;
else if(list1.get(i) > list2.get(j))
i++;
else{ // comman element
intersect.add(list1.get(i));
i++;
j++;
}
}
return intersect;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.