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 1063051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:43:33+00:00 2026-05-16T18:43:33+00:00

Hi I am trying to take two arrays and turn them into one 2

  • 0

Hi I am trying to take two arrays and turn them into one 2 dimensional array. However, I keep getting an error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
 at test5.sum(test5.java:12)
 at test5.main(test5.java:38)

Here is my code:

public class test5 {
    int[][] final23;

    public int[][] sum(int[] x, int[] y) {
        final23 = new int[2][x.length];
        for (int i = 0; i < final23[i].length; i++) {

            final23[1][i] = x[i];
            final23[2][i] = y[i];
        }
        return final23;
    }

    public void print() {
        for (int i = 0; i < final23[i].length; i++) {
            for (int j = 0; j < final23[i].length; j++) {

                System.out.print(final23[i][j] + " ");
            }
        }
    }

    public static void main(String[] args) {
        int l[] = { 7, 7, 3 };
        int k[] = { 4, 6, 2 };
        test5 X = new test5();

        X.sum(k, l);
        X.print();
    }
}

I am not really sure what the problem is. Sorry if the question is dumb, I am new to coding!

  • 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-16T18:43:34+00:00Added an answer on May 16, 2026 at 6:43 pm

    There is also a second problem with your program. You have this loop in both sum and print methods:

    for (int i = 0; i < final23[i].length; i++)
    

    In sum method it should be

    for (int i = 0; i < final23[0].length; i++)
    

    And in print method

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

    Otherwise you’ll get ArrayIndexOutOfBoundsException again.

    Note that the program works correctly only if both input arrays have the same length. This might be ok for your purposes, but keep that in mind.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to take a two-dimensional array and run it through a series
trying to take this content: <div class=content>one,two,three</div> <div class=content>four,five,six</div> <div class=content>seven,eight,nine</div> and .split and
I'm trying to take one group and put it in two different columns. I
I am trying to take one form type and display it however times i
I am trying to create a new array from two current arrays. Tried array_merge,
I'm trying to take two doubles (GPS coordinates) and send them over the ZigBee
I'm trying to get two different numbers from two different text files, put them
I have two 1D arrays, x & y, one smaller than the other. I'm
I'm trying to take an array, check if there are any dupes, and remove
Hi I am trying to split a string into an array of chars but

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.