This discussion is archived
8 Replies Latest reply: Mar 21, 2007 10:56 PM by 807606 RSS

Reverse Array

807606 Newbie
Currently Being Moderated
I have to make a program that reverses the integers in an array...I don't know why my code is not working??
public class ReverseArray
   {
     
       public static void main(String[] args) 
      {
         int[] array = new int [10];
           
         for (int i =0; i< array.length; i++){
            array[i] = (int)(100 + Math.random() * 100);
            System.out.print(array[i] + ", ");
         }
        
        
         int temp = 0;
        
         for (int i =0; i<array.length; i++){
            temp = array;

for (int j = array.length - 1; j<i; j--){
array [i] = array [j];
                         array[j] = temp;
}
}
     
System.out.println();

for (int k =0; k< array.length; k++)
System.out.print(array[k] + ", ");

     
}
}


Thanks for any input!!
  • 1. Re: Reverse Array
    807606 Newbie
    Currently Being Moderated
    Looks like you have an error here:
             for (int j = array.length - 1; j /* Error: ><i */;  j--){
    You have both a greater than, and a less than sign.

    Message was edited by:
    lethalwire
  • 2. Re: Reverse Array
    807606 Newbie
    Currently Being Moderated
    thanks for pointing that out... I just fixed it, but the output is not being reversed...

    its giving this output..

    119, 180, 172, 166, 150, 161, 128, 110, 168, 102,
    119, 180, 172, 166, 150, 161, 128, 110, 168, 102,
  • 3. Re: Reverse Array
    807606 Newbie
    Currently Being Moderated
    Well given the following lines:
             for (int i =0; i<array.length; i++){
                temp = array;

    for (int j = array.length - 1; j>i; j--){
    array [i] = array [j];
                   array[j] = temp;
    }
    }

    Let's say the array has 5 numbers, 1 through 5.
    Now, on the first iteration through the loop, the first value gets changed to the last position, and the last position gets changed to the first.

    So far so good.
    Now, on the next iteration, j becomes 3(second to the last spot of the array).
    Now, remember i is still set to zero. Now the first spot in the array becomes the second to the last spot in the array. Then the second to the last spot becomes the old first spot.

    I think you are a little confused on the 2 for loops.
    With 2 for loops, the inner for loop runs all the way through until false, then the outer loop increments or decrements, then the inner loop runs all the way through again. The pattern continues until both evaluate to false.
  • 4. Re: Reverse Array
    807606 Newbie
    Currently Being Moderated
    To reverse an array the best way is to use a single while loop.
    while(low < high) {
        swap elements
        increment low
        decrement high
    }
  • 5. Re: Reverse Array
    807606 Newbie
    Currently Being Moderated
    lethalwire:

    thanks for clearing the nested loops. I'm still new to programming. I thought that the code gets read first in the outer loop then in the inner loop and so on...I did not know the inner loop has to become false for it to jump out....

    flounder:

    a while loop sounds like a much simpler solution. I will try it that way...
  • 6. Re: Reverse Array
    807606 Newbie
    Currently Being Moderated
    I read a few tutorials on reversing an array about a year ago. I'd do a quick Google search on "reversing an array in java". That should result you in quite a few great tutorials on how other people have done this.

    If you need anymore help from there, feel free to ask.
  • 7. Re: Reverse Array
    807606 Newbie
    Currently Being Moderated
    I simply wonder why you are writing two for loops.
    Here is a simple logic:
    i be the first element in the array
    j be the last element in the array (ie., j = array.lenght -1)
    swap the elements at i and j (use your temp variable here)
    repeat this loop until i<j
    here is the code:
         for (int i =0, j= array.length -1; i<j; i++,j--){
              temp = array;
              array[i] = array[j];
              array[j] = temp;
         }
  • 8. Re: Reverse Array
    807606 Newbie
    Currently Being Moderated
    OK I ditched the for loops and created while loop...
    int temp;
             int low = 0 ;
             int high = array.length - 1 ;
             while (low < high) {
                temp = array[low];    
                array[low] = array[high];
                array[high] = temp;
                low++;
                high--;
             }
    This new code worked out great!

    Thanks for the help everyone...I appreciate it