1

I have an assignment to create a class in which I create an array of size 10, called source, and assign random integers in the range 0-10 to all indexes in it, and then call a method that creates a new array in reverse order. I tried the code below:

public class Exercise7_4 {

    // reverse array method
    public static int[] reverseArray(int[] arr) {
        int[] reverse = new int[arr.length];

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

        return reverse;
    }

    // print array in ascending order
    public static void printArray(int[] arr) {

        for (int i = 0; i < arr.length - 1; i++) {
            System.out.printf("%d\t", arr[i]);
        }

        System.out.println();
    }

    public static void main(String[] args) {
        int[] source = new int[10];

        for (int i = 0; i < source.length - 1; i++) {
            source[i] = (int) (Math.random() * 10 + 1);
        }

        int[] reverse = reverseArray(source);
        printArray(source);
        printArray(reverse);
    }
}        

The problem is that the output i get looks like this:

7   1   3   7   10  9   6   2   6   
0   6   2   6   9   10  7   3   1

meaning, the reverseArray method doesn't work properly on reverse[0] for some reason.

I would like to know why this is happening and how I can fix it. Thanks in Advance!

2
  • 1
    in your for loops you iterate from 0 to arr.length - 2 and reverse.length - 2. Change your loops to resemble for (int i = 0; i < reverse.length; i++) instead of for (int i = 0; i < reverse.length - 1; i++) Commented Dec 15, 2015 at 9:06
  • Thanks @JonnyHenly for the help! Commented Dec 15, 2015 at 9:17

4 Answers 4

2

change all your for-loops from

for (int i = 0; i < source.length - 1; i++)

to

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

Your reverse method is completely correct (if you change the loop). The mistake you made is that you created an array of size 10, filled it with 9 random values (the value of index 10 will therefore be 0).

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

Comments

2

You could check wich index are consulted in each iteration of your for loop.

This is your solution

    for (int i = 0; i < reverse.length - 1; i++) {
        System.out.println("" + i + " " +  (reverse.length - 1 - i));        
    }

And it prints:

0 9                                                                                                                                                                                                                                                    
1 8                                                                                                                                                                                                                                                    
2 7                                                                                                                                                                                                                                                    
3 6                                                                                                                                                                                                                                                    
4 5                                                                                                                                                                                                                                                    
5 4                                                                                                                                                                                                                                                    
6 3                                                                                                                                                                                                                                                    
7 2                                                                                                                                                                                                                                                    
8 1 

The 'i' don't get the value 9, and the (reverse.length - 1 - i) don't get the value 0.

Changing the testing condition for this:

i < reverse.length

gives you the last position:

9  0

Comments

1

Change reverseArray function as follows:

public static int[] reverseArray(int[] arr) {

int[] reverse = new int[arr.length];

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

    reverse[i] = arr[arr.length - i];

}


return reverse;

}

You need not to -1 from arr.length.

Change another for loop also:

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

    source[i] = (int) (Math.random() * 10 + 1);

}

Here, also you do not need to do -1 from source.length.

3 Comments

@JonnyHenly: Ya. But he has create array of 9 elements only. That's why.
i < source.length - 1 which means i<(11-1) which will turn into 9 elements.
Thanks @KrutiPatel that's worked. It was really a newbie mistake.
0

package com.test;

public class SortArray {

private int[] getSortedArry(int[] arr){

    int arrLen = arr.length;
    int reversedArry[] = new int[arrLen]; 
    System.out.println("array lenght: " +arr.length);
    int j = 0;
    for(int i=arrLen-1; i>=0; i--){

        reversedArry[j] = arr[i];


        j++;

    }
    System.out.print("\n");
    return reversedArry;

}

public static void main(String[] args){

    int arr[] = {10,2,3,4,5,12,23,43,45,65};

    SortArray sortArray = new SortArray();
    int reversedArry[] = sortArray.getSortedArry(arr);

    for(int i=0;i<reversedArry.length;i++){
        System.out.print(reversedArry[i] + " ");    

    }


}

}

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.