0

I tried:

public static void main(String[] args) {
    int array [] = {2, 5, 1, 4, 7, 9, 0};

    for (int i = 0; i < array.length-2; i++) {
        swapNum(array[i], array[i+2]);
    System.out.println(Arrays.toString(array));
    }

    System.out.println(Arrays.toString(array));
}

    public static void swapNum(int a, int b){

        int tmp = a;
        a = b;
        b = tmp;

    }

But found that it is not swapping values. Then I took help from here https://codereview.stackexchange.com/questions/86016/left-shifting-an-array-of-ints

public void anotherTry() {
    int nums [] = {4, 5, 2, 1, 6, 8};

    for (int i = 0, start = 0; i < nums.length; i++) {
        if (i == 0)
            start = nums[i];
        if (i == (nums.length - 1)) {
            nums[i] = start;
            break;
        }
        nums[i+2] = nums[i];
        System.out.println(Arrays.toString(nums));
    }

    System.out.println(Arrays.toString(nums));
}

Its gives array out of bound of exception.

  1. Where I am wrong?
  2. Role of start variable. If start always will be equal to nums[i]?
0

1 Answer 1

1

You can't swap values like that in Java. However, since you are using an array and want to swap values within the array, you can rewrite your swap method to work with indexes:

public static void main(String[] args) {
    int array [] = {2, 5, 1, 4, 7, 9, 0};

    for (int i = 0; i < array.length-2; i++) {
        swapNum(array, i, i+2);
    System.out.println(Arrays.toString(array));
    }

    System.out.println(Arrays.toString(array));
}
public static void swapNum(int[] values, int i, int j){

    int tmp = values[i];
    values[i] = values[j];
    values[j] = tmp;

}
Sign up to request clarification or add additional context in comments.

3 Comments

@paul - Well, yes, that's the logic you programmed into the main loop. I just showed how to swap values at two locations, which I thought was the point of this question. If you're now having problems figuring out how to get the overall logic right, you should perhaps post a separate question about that, explaining what you are trying accomplish, what you've tried, and what's going wrong (including expected and actual outputs).
Second code that I showed doesnt have the swap method. As per question there are 3 problems as we read 1. Question title: Right shift .... 2. whats the role of start var... 3. Where I am wrong...
@paul - Sorry, but the way I read your post is that the second code sample was a failed attempt to fix the problem that your original swap() method wasn't doing anything. You can use your original logic by running through the array in reverse order (varying i from array.length-1 down to 2) and swapping i and i-2 (instead of i and i+2). Basically, just do a mirror image of the current logic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.