Ask Question
6 May, 21:14

Write a method with the header public static void swapAdjacent (int[] values) that takes a reference to an array of integers values and swaps adjacent pairs of elements: values[0] with values[1], values[2] with values[3], etc. For example, if you add the following test code to your main method: int[] a1 = {0, 2, 4, 6, 8, 10}; swapAdjacent (a1); System. out. println (Arrays. toString (a1));

+5
Answers (1)
  1. 7 May, 00:37
    0
    public static void swapPairs (int[] a) {

    int len=a. length;

    if (len%2 = =0) {

    for (int i=0; i
    a[i]=a[i+1];

    a[i+1]=a[i];

    int[] b={a[i]+a[i+1]};

    }

    }

    if (len%2!=0) {

    for (int j=0; j
    a[j]=a[j+1];

    a[j+1]=a[j];

    a[len-1]=a[len-1];

    int[] b={a[j]+a[j+1]+a[len-1]};

    }

    }

    }

    public static void printArray (int[] a) {

    System. out. println (a);

    }
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “Write a method with the header public static void swapAdjacent (int[] values) that takes a reference to an array of integers values and ...” 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