1

Can anyone help with a homework problem I am doing? Basically the program I am trying to create asks the user for an integer N and then for two separate sets of N integers, these sets are stored in two separate arrays (x and y), and then a function mixReverse(a, b) is called with x and y as the parameters. The mixReverse(x,y) function reverses the order of the elements in the y array, and then interleaves the elements of each array into one final array so that the finalArray consists of arr1[0], arr2[arr2.length-1], arr1[1], arr2[arr2.length-2], ... arr1[arr1.length-1], arr2[0].

I am have tried this code, but it's problematic:

finalArray[0] = arr1[0];
    finalArray[1] = arr1[0];
    for (int j=2; j<(2*arr1.length); j++) {
        if (j%2==0) {
            finalArray[j] = arr1[j/2];
        }

        else {
            finalArray[j] = arr2[(((j+1)*(2))-1)];
        }
    }

Specifically this line: finalArray[j] = arr1[j/2];

is throwing the error:

java.lang.ArrayIndexOutOfBoundsException: 7
at lab2part1.mixReverse(lab2part1.java:33)
at lab2part1.main(lab2part1.java:15)

I know that the error is "Thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array." but I cant figure out why the index is illegal. Then length of the array is 2*(arr1.length), so this for loop should work properly. J is never negative and never equal to anything more than arr1.length -1 (which is equal to the final index of finalArray). Can anyone assist me in finding a solution?

9
  • What was the input on the above failure? Commented Mar 19, 2012 at 17:07
  • 3
    Where is finalArray initialized? Commented Mar 19, 2012 at 17:08
  • 1
    "Then length of the array is 2*(arr1.length)". The length of which array? Are you sure about the line of the error? Are you sure about the length of each array? Where is the code that sets up the arrays? Have you tried using a debugger? Have you tried using output statements (System.out.println or similar) to verify that things are as you think they are at various points in the execution? Have you tried anything at all? Commented Mar 19, 2012 at 17:09
  • 3
    Changing the snippet into a short but complete program which demonstrates the problem would be really helpful... Commented Mar 19, 2012 at 17:10
  • Can you show the definition of finalArray? Maybe you're accidentially creating it too small. Commented Mar 19, 2012 at 17:10

3 Answers 3

4

I would do it this way (without the unreadable mess of %2 and if statements)

int[] finalArray = new int[ 2 * N ];

for ( int i = 0; i < N; i++ ){

    finalArray[ 2 * i ] = array1[ i ];
    finalArray[ 2 * i + 1 ] = array2[ array2.length - i - 1 ];

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

1 Comment

Thank you, this is much cleaner, and I had not even thought about doing it this way.
2

This line:

finalArray[j] = arr2[(((j+1)*(2))-1)];

Looks suspect. You are using items 7,11,15, e.t.c. of arr2 which I don't think is what you want.

Comments

0

If arr1 has the same length of arr2, then this part is the problem:

arr2[(((j+1)*(2))-1)];

But scibuff has already written a good answer.

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.