Write a program that prints out the lists alter the followin

Write a program that prints out the lists alter the following methods are called. Method minToFront: takes an Array List of integers as a parameter. The method moves the minimum value in the list to the front, otherwise preserving the order of the elements. For 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 filter Range: accepts an Array List 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, 3, 7, 8, 6, 7] The call of filter Range (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 laud 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 {list1, list2} returns the list: [4, 11, 17, 28, 59] Print the list that is returned by this method.

Solution

Below is the program that implements all requirements. Program is tested fully and its working fine.To test each method just copy paste all program in your IDE/Compiler and remove comment character before method call.

If you have any doubt please so comments.

import java.util.*;
import java.io.*;
class HelloWorld
{
public static void main(String args[]) throws IOException
{
// System.out.println(myMethod());
ArrayList al=new ArrayList();
al.add(1);
al.add(3);
al.add(9);
al.add(7);
al.add(8);
al.add(4);


      
ArrayList tl=new ArrayList();
tl.add(1);
tl.add(2);
tl.add(6);
tl.add(9);
tl.add(10);
tl.add(15);
      
   Integer min = (Integer)3;
   Integer max = (Integer)5;
   //minToFront(al);      
   //filterRange(al,min,max);
   //intersect(al,tl);
}

public static void minToFront(ArrayList al)
{
Integer temp = (Integer)al.get(0);
int count = 0;
// System.out.println(temp);
for(int i=1;i<al.size();i++)
{
if((Integer)al.get(i) < temp)
{
temp =(Integer) al.get(i);
count = i;
}
}
al.remove(count);
al.add(0,temp);
  
Iterator itr=al.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}

     
public static void filterRange(ArrayList al,Integer min, Integer max)
{

for(int i=0;i<al.size();i++)
{
if((Integer)al.get(i) <= max && (Integer)al.get(i) >= min)
{
al.remove(i);
}
}
Iterator itr=al.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}

}


public static void intersect(ArrayList al,ArrayList tl)
{
ArrayList temp=new ArrayList();
for(int i=0;i<al.size();i++)
{
if(tl.contains(al.get(i)))
{
temp.add(al.get(i));
}
}
Iterator itr=temp.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}

}
}

 Write a program that prints out the lists alter the following methods are called. Method minToFront: takes an Array List of integers as a parameter. The method
 Write a program that prints out the lists alter the following methods are called. Method minToFront: takes an Array List of integers as a parameter. The method

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site