-3

As a beginner in Java I have a problem in reversing the array.

This is the code

public class Test {

    public static void main(String[] args) {

        int[] array = new int[] { 10, 20, 30, 40, 50, 60, 70, 80, 90 };
        int x = array.length;
        int[] y = new int[x];

        for (int i = 0; i < x / 2; i++) {
            int temp1 = array[i];
            y[i] = array[x - i - 1];
            array[i] = temp1;
            System.out.println(y[i]);
        }
    }
}

The result must be :

90, 80, 70, 60, 50, 40, 30, 20, 10

But I get only:

90, 80, 70, 60

How do I solve this problem?

Are there any good sources to teach Java?

  • 2
    Have a look at your code, especially at `i < x / 2`. Since `int x = array.length;` you are basically iterating over only half the array elements (and due to integer arithmetics it will be the smaller "half" if there are an odd number of elements like in your case). So change that to `i < x` and you should be good. And btw, this is something that should be easy to pick up with some debugging. – Thomas Nov 06 '18 at 16:47
  • Why use `x/2` ? – Nicholas K Nov 06 '18 at 16:47
  • 1
    When you reverse to the same array you should scan till the middle, but when you reverse to other array you must copy all. – SHR Nov 06 '18 at 17:05

3 Answers3

1

In your code, you are traversing only half of the original array,

public class Test {

public static void main(String[] args) {

    int[] array = new int[] { 10, 20, 30, 40, 50, 60, 70, 80, 90 };
    int x = array.length;
    int[] y = new int[x];

    for (int i = 0; i < x / 2; i++) { // traversing only half
        int temp1 = array[i]; // statement 1
        y[i] = array[x - i - 1]; // you just need this statement
        array[i] = temp1; //statement 2
        //Notice that statement 1 and 2 do nothing as a whole
        System.out.println(y[i]);

        }
    }
}

If you tweak your code like so,

import java.util.*;

public class MyClass {
    public static void main(String args[]) {
        int[] array = new int[] { 10, 20, 30, 40, 50, 60, 70, 80, 90 };
    int x = array.length;
    int[] y = new int[x];

    for (int i = 0; i < x; i++) {
        //int temp1 = array[i];
        y[i] = array[x - i - 1];
        //array[i] = temp1;

    }

    System.out.println(Arrays.toString(y));

    }
}

then it will work fine

mettleap
  • 1,390
  • 8
  • 17
-1

The for loop only iterates from i = 0 to i = array.Length/2, and since there is only one print statement inside its body, only elements in the first half of the array are printed.

AbsoluteSpace
  • 710
  • 2
  • 11
  • 21
-1
public class Test {

public static void main(String[] args) {

int[] array = new int[] { 10, 20, 30, 40, 50, 60, 70, 80, 90 };
int x = array.length;
int[] y = new int[x];

for (int i = 0; i < x; i++) {
    y[i] = array[x - i - 1];
    System.out.println(y[i]);
    }
}

}

since you need whole array to get reversed, you don't need to iterate only upto x/2, while iterating upto x(i.e the actual length of array) will give you desired results.

For java tutorials, you can learn it here

Asim
  • 161
  • 1
  • 12