Ask Question

RrayList Mystery Consider the following method:

public static void mystery (ArrayList list) {

for (int i = 1; i < list. size (); i + = 2) {

if (list. get (i - 1) > = list. get (i)) {

list. remove (i);

list. add (0, 0);

}

}

System. out. println (list);

} Write the output produced by the method when passed each of the following ArrayLists: List Output

a) [10, 20, 10, 5]

b) [8, 2, 9, 7, - 1, 55]

c) [0, 16, 9, 1, 64, 25, 25, 14

+4
Answers (1)
  1. 9 April, 23:08
    0
    a)

    int: [10, 20, 10, 5]

    out: [0, 10, 20, 10]

    b)

    int: [8, 2, 9, 7, - 1, 55]

    out: [0, 0, 8, 9, - 1, 55]

    c)

    int: [0, 16, 9, 1, 64, 25, 25, 14]

    out: [0, 0, 0, 0, 16, 9, 64, 25]

    Explanation:

    What the code is doing is changing the list if and only if the item number i, where i is an odd position on the list, is greater than the item number i-1. If the condition mentioned is satisfied, the element number i is removed, and a zero is added to the beginning of the list; this is why the dimension of the input is equal to the output (for every element removed a zero is added).

    Note:

    The first item of a list is the item 0. add method can receive two parameters the first indicating the position where the element will be added and the second the element to be added ... get method allows you to select any element on the list by its index ... size method gives you the size of the list In the for loop i + = 2 indicates an increment of two, since we start with i = 1 we are going to iterate through odd numbers i = 1,3,5,7 ...
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “RrayList Mystery Consider the following method: public static void mystery (ArrayList list) { for (int i = 1; i < list. size (); i + = 2) { ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers