1

The error is an ArrayIndexOutOfBoundsException which I understand yet I don't know where the error is on the code.

public class Main {
    public static void main(String[] args) {
    double[] testArray = new double[]{3.5, 7.9, 0.0, -7.9, 10.99, 78.9, 66.8, 19.01, 18.9,99.7}; //declaring desired array

    double[] reversedTArray = new double[10]; // Using variable to reverse the first array

        for (double i = 1; i < testArray.length;i++ ){

            reversedTArray[1-(int)i] = testArray[10-(int)i];
        }

        for (double i : reversedTArray){
            System.out.println(reversedTArray[(int)i]);
        }
    }
}

I'm simply attempting to reverse an array by assigning the reversed one to a new array using a for loop.I expected the values to reverse but it just showed me an error.

The result I want would be this:

99.7
18.9
19.01
66.8
78.9
10.99
-7.9
1.0
7.9
3.5
LinFelix
  • 1,026
  • 1
  • 13
  • 23
Turbo
  • 23
  • 5
  • 5
    *"I don't know where the error is on the code"* - This is a good opportunity for you to start familiarizing yourself with [using a debugger](https://stackoverflow.com/q/25385173/328193). When you step through the code in a debugger, which operation first produces an unexpected result? What were the values used in that operation? What was the result? What result was expected? Why? – David May 02 '23 at 14:18
  • i goes from 1 to 9, ask yourself what `reversedTArray[1-(int)i]` is going to be at various values... – teapot418 May 02 '23 at 14:19
  • 3
    ... and it's also a good opportunity to learn [how to read stack traces](https://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors). After all, the error tells you exactly where the error occurred in your program. – meriton May 02 '23 at 14:21
  • And the `for(i:X)` construct is already giving you the contents of the array in `i`, just print that, do not use it as index. – teapot418 May 02 '23 at 14:23

3 Answers3

3

Well, you are mixing up indexing and values. The index should be an integer, and the value a float.

Note: Indexing always starts at 0 and ends at length - 1.

public class Main {
    public static void main(String[] args) {
        double[] testArray = new double[] {
            3.5, 7.9, 0.0, -7.9, 10.99, 78.9, 66.8, 19.01, 18.9, 99.7
        };
        double[] reversedArray = new double[testArray.length]; // Use length of original

        // Loop through each index
        for (int i = 0; i < testArray.length; i++) {
            reversedArray[i] = testArray[testArray.length - i - 1];
        }

        // Iterate through the values
        for (double value : reversedArray) {
            System.out.println(value);
        }
    }
}

Output

99.7
18.9
19.01
66.8
78.9
10.99
-7.9
0.0
7.9
3.5

Alternatively, you can assign the reverse value by indexing backwards like so:

for (int i = testArray.length - 1; i >= 0; i--) {
    reversedArray[testArray.length - i - 1] = testArray[i];
}

If you want to swap in-place and don;t care about mutating the original array, you can try a swap.

This will run at O(n/2):

public class Main {
    public static void main(String[] args) {
        double[] testArray = new double[] {
            3.5, 7.9, 0.0, -7.9, 10.99, 78.9, 66.8, 19.01, 18.9, 99.7
        };

        for (int i = 0; i < testArray.length / 2; i++) {
            // Perform a swap
            double tmp = testArray[testArray.length - i - 1];
            testArray[testArray.length - i - 1] = testArray[i];
            testArray[i] = tmp;
        }

        for (double value : testArray) {
            System.out.println(value);
        }
    }
}
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
2

For loops are composed by three main elements: the iterator, the conditional and the operation to be executed each time. The usual way to use for loops is to start by zero and increment until it reaches the length of the object.

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

But there's other forms to iterate over a array. One of them is going the opposite way: starting in the last element of the array and go on until the value is zero:

for (int i = myArray.length - 1; i >= 0; i--) 

In this code, i starts with the length of the array (10). Then, each time the for is executed, i is decreased in one, until i equals to zero. That way, you can go through a array in the reverse order.

Complete code:

public static void main(String[] args) {
    double[] testArray = new double[]{3.5, 7.9, 0.0, -7.9, 10.99, 78.9, 66.8, 19.01, 18.9,99.7};
    
    double[] reversedTArray = new double[10];
    int reversedCounter = 0;  // index of the new array;
    
    for (int i = testArray.length - 1; i >= 0; i--) {
        reversedTArray[reversedCounter] = testArray[i];
        reversedCounter++;
    }
    
    for (int i = 0; i < reversedTArray.length; i++) {
        System.out.println(reversedTArray[i]);
    }
}
Tandera
  • 54
  • 3
-1
public class Main {
public static void main(String[] args) {
    double[] testArray = new double[]{3.5, 7.9, 0.0, -7.9, 10.99, 78.9, 66.8, 19.01, 18.9,99.7}; //declaring desired array

    double[] reversedTArray = new double[10]; // Using variable to reverse the first array

        for (double i = 0; i < testArray.length;i++ ){

            reversedTArray[(int)i] = testArray[10-1-(int)i];
        }

        for (double i : reversedTArray){
            System.out.println(i);
        }
}

}

This will work.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 03 '23 at 06:02