anonymous
  • anonymous
public class tester { public static void main(String [] args){ int[]gamma=new int[4]; for(int i=0; i<4; i++) gamma[i] = i+3; System.out.print(gamma[4]); } } Why is it out of bounds?
Computer Science
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
jamiebookeater
  • jamiebookeater
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
eSpeX
  • eSpeX
At first glance I would say it is because you are printing gama[4] when only 0-3 exist.
anonymous
  • anonymous
arrays use zero based referencing, so if you want the 4th element you'd say gamma[3], if you want the first you'd say gamma[0]
anonymous
  • anonymous
You are asking it to print gamma[4] when it simply doesn't exist! You created an array of size 4 when you said int[]gamma=new int[4] This means you created an array that has indexes 0,1,2,3. Index 4 does not exist. If you wanted to print index 4 you would have had to create an array of size 5 ie(int[] gamma= new int[5] Hope this helps!

Looking for something else?

Not the answer you are looking for? Search for more explanations.

More answers

anonymous
  • anonymous
The arrays in Java are Zero-Index based, i.e. they start with 0 index and end in [ Total Items - 1 ], So, your case you're getting Out-Of-Bounds error as the gamma[4] is exceeding the last index of your array.

Looking for something else?

Not the answer you are looking for? Search for more explanations.