Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2013
    Posts
    33
    Rep Power
    0

    Default 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]);
          } 
        } 
    }

  2. #2
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default 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.

  3. #3
    Join Date
    Feb 2013
    Posts
    33
    Rep Power
    0

    Default Re: Array; error question?

    Do I need to make x[i] the same length as y?

  4. #4
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default Re: Array; error question?

    Quote Originally Posted by wheehoowaffles View Post
    Do I need to make x[i] the same length as y?
    Long answer:
    Think logically. Your x is length of 1 and each loop iteration you want to add it into the next index and you loop 3 times.

    Short answer:
    Try and see what happens.
    Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

  5. #5
    Join Date
    Feb 2013
    Posts
    33
    Rep Power
    0

    Default Re: Array; error question?

    Is this right?

    Java Code:
    public class Reverse {
       public static void main( String[] args)
       {
          
          int[] y = { 1, 2, 3};
          int[] x = { 1, 2, 3};
                  
          for ( int i = 0; i < y.length; i++ ) {
    	x[i] = y[2-i];
               
               System.out.println( "contents of integer array x = " + x[i] );
          } 
        } 
    }

Similar Threads

  1. Array question
    By Jbo in forum New To Java
    Replies: 3
    Last Post: 09-02-2012, 09:46 AM
  2. Array Question
    By mamba24 in forum New To Java
    Replies: 6
    Last Post: 06-04-2012, 09:59 PM
  3. Question about a 2d array example
    By silverglade in forum New To Java
    Replies: 8
    Last Post: 06-07-2011, 12:56 AM
  4. Array question..
    By gerarda in forum New To Java
    Replies: 14
    Last Post: 03-02-2011, 02:42 AM
  5. array question
    By dazednconfused in forum New To Java
    Replies: 4
    Last Post: 09-15-2009, 06:44 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •