0

I'm trying to invert an array, with the below-shown code, with no sucess. The output of the program is 7,0,0 and not 7,1,2 as its supposed to be.

Code:

import java.util.Arrays;

public class ReverseArray
{
    public static void main(String[] args)
    {
        int[] data = {1, 2, 7};
        int[] dataR = reverseArray(data);
        System.out.println("Original Array: " + Arrays.toString(data));
        System.out.println("Reverse Array: " +  Arrays.toString(dataR));
    }
    public static int[] reverseArray(int[] data)
    {
        int[] reversedData = new int[data.length];
        int i;
        for(i=0; i < data.length; i++);
        {
            reversedData[i] = data[(data.length - i -1)];
        }
        return reversedData;
    }
}

All help appreciated, thanks.

Perception
  • 79,279
  • 19
  • 185
  • 195
rjpedrosa
  • 652
  • 2
  • 7
  • 10

6 Answers6

8

This is your problem:

for(i=0; i < data.length; i++);

Delete the ; . The way you wrote it, first there's a loop that counts i up to data.length, then a scoped block that tries to access reversedData[data.length] exactly once. That won't fly.

Funny thing: originally, probably none of us saw what the problem is; using formatting rules in an IDE will tell you pretty quickly what's wrong: you'll see that your code doesn't look like it's supposed to when formatted according to rules you're used to.

G. Bach
  • 3,869
  • 2
  • 25
  • 46
0

You should use temporary variable to swap values in array. For example like this:

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

or

ArrayUtils.reverse(int[] array)

All described in this question.

Community
  • 1
  • 1
Iwo Kucharski
  • 3,735
  • 3
  • 50
  • 66
0

Good catch, G. Bach. I'm also not sure about the first part of your question. It looks like the original array is 1, 2, 7. Wouldn't the reverse of that be 7, 2, 1 and NOT 7, 1, 2?

That said, it seems like you might be slowing things down by accessing data.length every time. I'd probably create a local variable thusly:

public static int[] reverseArray(int[] data)
{
    int arr_length = data.length
    int[] reversedData = new int[arr_length];
    int j = arr_length - 1;
    for(int i=0; i < arr_length; i++);
    {
        reversedData[i] = data[j--];
    }
    return reversedData;
}

Notice how j is automatically decremented after it's been accessed, keeping things nice and tidy. Also, we've saved the length of the array into a local variable so it's more efficient to access.

Onikoroshi
  • 281
  • 4
  • 16
  • Just let the JVM's JIT handle it. ('sides, you're still accessing `data.length` each time in the `i < data.length`). – nneonneo Mar 08 '13 at 01:11
  • Oh yeah, because the length could have changed during the body of the loop, and so it would have to check. Good point. ^^; – Onikoroshi Mar 11 '13 at 15:25
0
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner in=new Scanner(System.in);
        int size=in.nextInt();
        int array[]=new int[size];

        for(int i=0;i<size;i++){
            array[i]=in.nextInt();
        }
        for(int i=size-1;i>=0;i--)
        {
            System.out.print(array[i]+" ");
        }


        }

        }
meshsf
  • 163
  • 2
  • 14
0
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class ReverseArray 
{

    public static void main(String[] args) 
    {

        System.out.println("Enter the array Size");
        Scanner in=new Scanner(System.in);
        int size=in.nextInt();
        System.out.println("Enter the array Elements");
        int array[]=new int[size];

        for(int i=0;i<size;i++){
            array[i]=in.nextInt();
        }
        System.out.println("reverse of entered are given below for Size : " +size );
        for(int i=size-1;i>=0;i--)
        {
            System.out.print(array[i]+" ");
        }


        }

}
0

Input:

Enter the number of values to reverse 7

1 2 3 4 6 7 8

Output: Reversed Array :[8, 2, 3, 4, 6, 7, 1]

Reversed Array :[8, 7, 3, 4, 6, 2, 1]

Reversed Array :[8, 7, 6, 4, 3, 2, 1]

Coding:

public class Test{

    public static void main(String[] args){

         System.out.println("Enter the number of values to reverse");

         Scanner sc = new Scanner(System.in);

         int n  = sc.nextInt();
         int num[] = new int[n];    
         for(int i=0; i < num.length; i++) 
             num[i] = sc.nextInt();

         reverseArray(num, n-1);

    }

    private static void reverseArray(int num[], int end) {
        int start = 0;
        int temp;

        while(start < end) {
            temp = num[start];
            num[start] = num[end];
            num[end] = temp;
            start++;
            end--;
            System.out.println("Reversed Array :"+ Arrays.toString(num));

        }


    }


}
Stephen
  • 9,899
  • 16
  • 90
  • 137