Khanday Ovais
Khanday Ovais

Reputation: 15

Returning an array from a method in java?

Why I am not getting any output on my output screen? There should be a reversed array.

public class Main {
    static int[] reverse(int[] array)
    {
        int[] result =new int[array.length];
        for(int i=0,j=array.length-1;i<array.length;i++,j--)
        {
            result[j]=array[i];
        }
        return result;
    }


    public static void main(String[] args) {

        int[] newArray = {1,5,9,7,8,0,3,2};
        reverse(newArray);
    }
}

Upvotes: 0

Views: 48

Answers (1)

Bill Horvath
Bill Horvath

Reputation: 1717

If you want to see output in your console, you need to send it to either the system's output stream (appropriate in this case), or the error stream.

int[] newArray = {1,5,9,7,8,0,3,2};
newArray = reverse(newArray);     // <- reuse the variable to save memory
System.out.println(Arrays.toString(newArray));

Upvotes: 2

Related Questions