Ask Question
11 June, 15:42

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));

+2
Answers (1)
  1. 11 June, 18:33
    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?
Find 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 and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers