0

I have this extremely simple program to reverse an array of integers but every time I try to run it, it just ignores the second loop where the reverse should happen and passes it or there maybe some trouble in it.

package chapter_1;

import java.util.*;

public class Reverse_array {

    public static void main(String[] args) {
        Scanner keybd = new Scanner(System.in);

        int[] arr = new int[10];

        for (int i = 0; i < arr.length; i++) {
            System.out.println("[%" + i + "]=");
            arr[i] = keybd.nextInt();
        }

        for (int i = 0; i < arr.length; i++) {
            int temp = arr[i];
            arr[i] = arr[10 - 1 - i];
            arr[10 - 1 - i] = temp;
        }

        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]);
        }
    }
}
  • 3
    Your exchange is going on for too much elements (`i = 0` swaps head an tail of the array, `i = 9` swaps them back) –  Mar 22 '14 at 10:16
  • @RC. I completely forgot about that Thank you –  Mar 22 '14 at 10:18

5 Answers5

0

it does not ignore the second loop, it simply should be

   for (int i = 0; i < arr.length/2; i++) {
        int temp = arr[i];
        arr[i] = arr[10 - 1 - i];
        arr[10 - 1 - i] = temp;
    }

beacuse if you iterate from 0 to arr.length it will reverse Your array twice which in the end gives You back original array,

pezetem
  • 2,503
  • 2
  • 20
  • 38
0

Here is the problem

for (int i = 0; i < arr.length; i++) {
        int temp = arr[i];
        arr[i] = arr[10 - 1 - i];
        arr[10 - 1 - i] = temp;
}

You make twice array elements swap

Instead, use

for (int i = 0; i < arr.length / 2; i++) {
    int temp = arr[i];
    arr[i] = arr[arr.length - 1 - i];
    arr[arr.length - 1 - i] = temp;
}
djm.im
  • 3,295
  • 4
  • 30
  • 45
0

Just change arr.length to arr.length/2. Now you are reversing it twice.

0

You should browse your array until his half for not reversing twice ( -- => +).

Always try to use the array size to the browse.

for (int i = 0; i < arr.length; i++)

and not

for (int i = 0; i < 10; i++)

It's more general and correct. You can add a variable to save the array size. So your code will:

package chapter_1;

import java.util.*;

public class Reverse_array {

    public static void main(String[] args) {
        Scanner keybd = new Scanner(System.in);
        int arrayLenght = 10;
        int[] arr = new int[arrayLenght];

        for (int i = 0; i < arrayLenght; i++) {
            System.out.print("%[" + i + "]= ");
            arr[i] = keybd.nextInt();
        }

        for (int i = 0; i < arrayLenght / 2; i++) {
            int temp = arr[i];
            arr[i] = arr[arrayLenght - 1 - i];
            arr[arrayLenght - 1 - i] = temp;
        }

        for (int i = 0; i < arrayLenght; i++) {
            System.out.println(arr[i]);
        }
    }
}
0

I see that others gave you the solution as i'm writing.

Let me give you some suggestions: -whatever you are trying, magane to keep the developing loop (error, modification, trial and again from start) as short as possible.

-insert data by hand rarely is a good option

so, in this case: better to avoid the first loop and put something like:int [] arr ={5,6,7,8,8,1,8,9,1,9};

-when in doubt fill the code with print so you can follow what the algorithm is doing,the next step will be learn to use the debugger but the first is the print instructions

-if you want to learn, try hard enough (it depends by you) before ask other's people help

chairam
  • 335
  • 5
  • 12
  • well thank you for your advice that has nothing to do with the question and i want to pint that i'm not completely new to the programming domain i'm almost 2 years in by now. –  Mar 22 '14 at 10:52