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.

2
  • 4
    Are you required to do it by hand? Can you use something like Collections.reverse(Collections.toArray(data)).toArray(new int[data.length]) ? Commented Mar 8, 2013 at 0:26
  • @BheshGurung I did say "something like" :P - trying to ascertain if it was homework or not ;) Commented Mar 8, 2013 at 0:40

6 Answers 6

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.

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

3 Comments

It also needs to stop half way to data.length or it reverses the array then unreverses it :P
@Patashu: No, it is building a new array, not swapping.
Ah, my mistake for skimming :)
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.

1 Comment

Ehem, he isn't swapping anything, he's building a brand new array.
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.

2 Comments

Just let the JVM's JIT handle it. ('sides, you're still accessing data.length each time in the i < data.length).
Oh yeah, because the length could have changed during the body of the loop, and so it would have to check. Good point. ^^;
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]+" ");
        }


        }

        }

Comments

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]+" ");
        }


        }

}

1 Comment

Please also add some explanation to your answer.
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));

        }


    }


}

Comments

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.