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

  • Home
  • SEARCH
  • 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 6698281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:33:34+00:00 2026-05-26T06:33:34+00:00

Running this code gives me an array out of bounds exception at the line:

  • 0

Running this code gives me an array out of bounds exception at the line:

int sum = array[k]+array[l]; //sum of l and k

…Should be a simple fix, but I can’t figure out what could be causing it, given I’m using array.length to bound the loop. Can anyone help?

P.S. For the record, this code is supposed to search an int array for pairs of ints or single ints that equal to a target int. It works using solely the println’s, but I’m trying to put the numbers that add up to the target into vectors.

public Vector<Vector<Integer>> subsetSum(int[] array, int target) {
    //creates vectors, adds inner vector to another vector
    outer = new Vector<Vector<Integer>>();
    inner = new Vector<Integer>();
    outer.add(inner);

    for (int k = 0; k <= array.length; k++) {
        for (int l = 0; l <= array.length; l++) {
            int sum = array[k]+array[l]; //sum of l and k
            int i = 0;

            if (sum == target) {

                inner.add(i, array[l]);
                inner.add(i, array[k]);

                i++;

                //prints combination
                System.out.println(array[l]+"+"+array[k]+"="+target);
            }
            if (k == target) {
                inner.add(i, array[k]);
                i++;
                //prints if int equals target
                System.out.println(k+"="+target);
            }
            if (l == target) {
                inner.add(i, array[l]);
                i++;
                //prints if int equals target
                System.out.println(l+"="+target);
            }
        }
    }
    //return combinations that add up to target in vector form
    System.out.println(outer);
    return outer;
}
  • 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-26T06:33:35+00:00Added an answer on May 26, 2026 at 6:33 am

    You need to use “<” instead of “<=” in your for loops.

    Since the first position in the array is 0, the last position is length-1. What’s happening is that when you reach the last iteration, the index is already out of the bounds of the array.

    For instance, if you have an array:

    array = [0,1,2,3] the last iteration would be array[4], the length of the array, which is out of the bounds.

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

Sidebar

Related Questions

I'm running code that sometimes yields this: UInt32 current; int left, right; ... //sometimes
I've tried writing this code from scratch, coding, and running it but it just
I'm a beginner with jdbc ... I have a problem running this code :
I'm running this C# code in Visual Studio in debug mode: public class MyHandlerFactory
Running the static analyzer on this piece of code: - (id) readForeignPref { CFPropertyListRef
Running FxCop on my code, I get this warning: Microsoft.Maintainability : 'FooBar.ctor is coupled
Code is not running on .click when I have this: $(.cancel).click(function() { alert(got here);
Similar to this question , after running the following code the browser dialog does
Following the directions at this question , I have some code running to extract
To start out with - this is not my code - there was a

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.