Swapping adjacent numbers in arrays

14,875

Solution 1

After almost breaking my computer several times, here's the actual working code:

public static void swapPairs(int[] a){
    int len=a.length;
        if(len%2 ==0){
            for(int i=0; i<len; i=i+2){
                int c=a[i]+a[i+1];
                a[i]=c-a[i];
                a[i+1]=c-a[i+1];
            }   
        }
        if(len%2 !=0){
            for(int j=0; j<len-1; j=j+2){
                int c=a[j]+a[j+1];
                a[j]=c-a[j];
                a[j+1]=c-a[j+1];
            }   
            a[len-1]=a[len-1];
        }   
}
public static void printArray(int[] a){
    int len=a.length;
    for(int i=0;i<len;i++)
        System.out.print(a[i]+" ");
}

Solution 2

What you need to print is Arrays.toString(a)

Now, you are just printing the Hashcode of your Array object

Solution 3

First, your swap method could be simplified. Add both numbers together, and then subtract each from the sum (to get the other number). Something like,

public static void swapPairs(int[] a) {
    for (int i = 0; i < a.length - 1; i += 2) {
        int c = a[i] + a[i + 1];
        a[i] = c - a[i];
        a[i + 1] = c - a[i + 1];
    }
}

Then you could use Arrays.toString(int[]) to get a String. Like,

public static void printArray(int[] a) {
    System.out.println(Arrays.toString(a));
}

I tested the above like

public static void main(String[] args) {
    int[] t = { 1, 2, 3, 4 };
    printArray(t);
    swapPairs(t);
    printArray(t);
}

And I got

[1, 2, 3, 4]
[2, 1, 4, 3]
Share:
14,875
Maria
Author by

Maria

"Just a gal trying to understand the complexity of tech"

Updated on August 03, 2022

Comments

  • Maria
    Maria over 1 year

    Here's the problem: Write a method called swapPairs that accepts an array of integers and swaps the elements at adjacent indexes. That is, elements 0 and 1 are swapped, elements 2 and 3 are swapped, and so on. If the array has an odd length, the final element should be left unmodified. For example, the list {10,20,30,40,50} should become {20,10,40,30,50} after a call to your method.

    Write method printArray that is passed an array and will print out each element.
    Use this method to print the array modified by swapPairs.

    This is my code:

    public static void swapPairs(int[] a){
        int len=a.length;
            if(len%2 ==0){
                for(int i=0; i<len; i=i+2){
                    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<len; j=j+2){
                    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);
    }
    

    However, what it returns is [I@2a139a55

  • Rarblack
    Rarblack over 5 years
    Add some explainatins or comment to your answer like what you change and why you did this.
  • QuantumDeveloper
    QuantumDeveloper about 3 years
    Welcome to stackoverflow. Before posting an answer you should ask yourself these fundamental questions: | Is my answer showing a different solution to all the other answers?(the swap and print functionality are similar to multiple other answers) | Is an answer still relevant to the community?(the question was asked 5 years ago) | Is every part of my answer relevant to the question?(You use a scanner to read the array while the OP specifically said "a method […] that accepts an array of integers". Therefor the method of getting that array is irrelevant for the question.)
  • Christoph S.
    Christoph S. over 2 years
    This solution is a bit complicated
  • Christoph S.
    Christoph S. over 2 years
    Why do you use the for condition ì < number.length` and later in that loop the if condition i > number.length - 2?