Results 1 to 5 of 5
Thread: Array; error question?
- 04-05-2013, 01:05 AM #1
Member
- Join Date
- Feb 2013
- Posts
- 33
- Rep Power
- 0
Array; error question?
I'm not sure if I this this right but when I run the code I get this:
contents of integer array x = 3
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at Reverse.main(Reverse.java:9)
I don't know what this mean so I was wondering if someone could explain it to me. Thanks in advance.
What I'm asked to do:
Given that the integer array y has the following elements : [1 2 3], what is the contents of integer array x after executing the following statements :
for (int i = 0; i < 3; i++) x[i] = y[2-i];
Java Code:public class Reverse { public static void main( String[] args) { int[] y = { 1, 2, 3}; int[] x = {0}; for ( int i = 0; i < 3; i++ ) { x[i] = y[2-i]; System.out.println( "contents of integer array x= " + x[i]); } } }
- 04-05-2013, 01:44 AM #2
Senior Member
- Join Date
- Feb 2010
- Posts
- 128
- Rep Power
- 0
Re: Array; error question?
On the second loop iteration it tries to access index x[1] and your x is what length?
EDIT:
ArrayIndexOutOfBoundsException is the program telling you: I am trying acces index 3, but your array is of length 2. So what up with that?Last edited by FlyNn; 04-05-2013 at 01:50 AM.
Measuring programming progress by lines of code is like measuring aircraft building progress by weight.
- 04-05-2013, 02:12 AM #3
Member
- Join Date
- Feb 2013
- Posts
- 33
- Rep Power
- 0
Re: Array; error question?
Do I need to make x[i] the same length as y?
- 04-05-2013, 02:16 AM #4
Senior Member
- Join Date
- Feb 2010
- Posts
- 128
- Rep Power
- 0
Re: Array; error question?
Measuring programming progress by lines of code is like measuring aircraft building progress by weight.
- 04-06-2013, 03:01 AM #5
Member
- Join Date
- Feb 2013
- Posts
- 33
- Rep Power
- 0
Similar Threads
-
Array question
By Jbo in forum New To JavaReplies: 3Last Post: 09-02-2012, 09:46 AM -
Array Question
By mamba24 in forum New To JavaReplies: 6Last Post: 06-04-2012, 09:59 PM -
Question about a 2d array example
By silverglade in forum New To JavaReplies: 8Last Post: 06-07-2011, 12:56 AM -
Array question..
By gerarda in forum New To JavaReplies: 14Last Post: 03-02-2011, 02:42 AM -
array question
By dazednconfused in forum New To JavaReplies: 4Last Post: 09-15-2009, 06:44 AM
Bookmarks