Ask Question

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

+3
Answers (1)
  1. 3 July, 20:40
    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 & 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