By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,648 Members | 1,489 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,648 IT Pros & Developers. It's quick & easy.

array out of bounds exception

P: 1
Everytime i input a number it gives me the array out of bounds Eception error : 3 and hihglights the product = product * A{i} bit i would be grateful for any help.

public class Accumulator {
private int[] A;

public Accumulator(int[] X) {
A= new int[X.length];
for (int i=0; i<X.length; i++)
A[i] = X[i];

public int prodA(int m) {

int product= 1;

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

product = product * A[i];

return product;

return m;

if (product == 0)
return 0;

return 0;


Aug 15 '12 #1
Share this Question
Share on Google+
1 Reply

Expert 100+
P: 785
look at "A.length+1" in your source code. You have to correct that. Why?

Assume, the length of A is 3.
So what is the value of the index i in the last iteration of your loop?
(Yes, it's 3!)
But you can only access A[0], A[1] and A[2]. (3 elements). and it crashes with an index-out-of-bounds-exception when you try to acces A[3].
Sep 12 '12 #2

Post your reply

Sign in to post your reply or Sign up for a free account.