Ask Question
16 September, 01:44

2. Write a program that calls creates an ArrayList of integer and calls the method minToFront described below. Print out the list before and after the method call Method minToFront: takes an ArrayList 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.

+4
Answers (1)
  1. 16 September, 02:24
    0
    import java. util. ArrayList;

    public class MinToFront {

    public static void minToFront (ArrayList list) {

    int miniIndex = 0;

    for (int j=1; j
    if (list. get (miniIndex) > list. get (j))

    miniIndex = j;

    }

    / / moving min to front

    int min = list. get (miniIndex);

    list. remove (miniIndex);

    list. add (0, min);

    }

    public static void main (String[] args) {

    ArrayList list = new ArrayList ();

    list. add (4); / /value store in list

    list. add (7); / /value store in list

    list. add (9); / /value store in list

    list. add (2); / /value store in list

    list. add (7); / /value store in list

    list. add (7); / /value store in list

    list. add (5); / /value store in list

    list. add (3); / /value store in list

    list. add (5); / /value store in list

    list. add (1); / /value store in list

    list. add (7); / /value store in list

    list. add (8); / /value store in list

    list. add (6); / /value store in list

    list. add (7); / /value store in list

    System. out. println (list);

    minToFront (list);

    System. out. println (list);

    }

    }

    Output:

    [1, 4, 7, 9, 2, 7, 7, 5, 3, 5, 7, 8, 6, 7]

    Explanation:

    Firstly, we define a method minToFront (), than set an integer variable "miniIndex" to 0, than set a for loop and apply the following condition and inside the loop we apply the if condition, set an integer variable "min" in which we store the value and than we set the following list [4, 7, 9, 2, 7, 7, 5, 3, 5, 1, 7, 8, 6, 7], after that print the list and than print an output.
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “2. Write a program that calls creates an ArrayList of integer and calls the method minToFront described below. Print out the list before ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers