Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7668501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:22:48+00:00 2026-05-31T15:22:48+00:00

Can anyone help with a homework problem I am doing? Basically the program I

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T15:22:50+00:00Added an answer on May 31, 2026 at 3:22 pm

    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 ];
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

can anyone help me with my problem. I need to create a sql rule
Can anyone help me with the trying to write SQL (MS SqlServer) - I
can anyone help me in trying to check whether JavaScript is enabled in client
Can anyone help me to create a simple viewing of cam using just a
Can anyone help me create an android layout like the image below. It has
Can anyone help me with this nonsense problem? <?php if(!readfile($path)) { $error = error_get_last();
Can anyone help me out with this query i m trying to execute, static
Can anyone help me to get Hotmail contact list by passing user credentials. any
Can anyone help with with the time complexity of this algorithm, and why it
Can anyone help? I have been designing a site using Javascript but the rest

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.