Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: ArrayIndexOutOfBoundsException

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default ArrayIndexOutOfBoundsException

    Hi,

    I have this code which is meant to reverse the array a, however I get a message ArrayIndexOutOfBoundsException - why is this, and how do I correct the code to not get this problem?

    public class MirrorElements {
        /**
         * Reverse the array a
         * Error message: ArrayIndexOutOfBoundsException
         * @param args
         */
         public static void main (String[] args) {
             int i = 0;
             while (i < a.length /2) {
                 int original = a[i]; // swap a[i] and a[a.length - i]
                 a[i] = a[a.length - i];
                 a[i.length - i] = original;
                 i = i + 1;
             }
         }
     
    }

  2. #2
    Member
    Join Date
    May 2010
    Posts
    36
    Thanks
    0
    Thanked 13 Times in 12 Posts

    Default Re: ArrayIndexOutOfBoundsException

    Quote Originally Posted by NightFire91 View Post
    Hi,

    I have this code which is meant to reverse the array a, however I get a message ArrayIndexOutOfBoundsException - why is this, and how do I correct the code to not get this problem?
    you get an ArrayIndexOutOfBoundsException because the index of an array is zero-based and the length field of the array is not zero-based. so if you want to get the last char in the array you must use a[length - 1]. here is the corrected code:

    public class MirrorElements {
     
    	public static void main(String[] args) {
    		char[] a = { 'h', 'a', 'l', 'l','o',' ', 'w','o', 'r','l','d'};
    		StringBuilder sf = new StringBuilder(new String(a));
     
    		int i = 0;
    		while (i < a.length / 2) {
    			int original = a[i]; // store a[i] before swapping
    			// swap a[i] with length - i -1
    			a[i] = a[a.length - i -1];
    			// store the original char
    			a[a.length - i -1] = (char)original;
    			i = i + 1;
    		}
    		System.out.println(a);
    		// the StringBuffer has a reverse() method, so
    		// use it to check if your a[] is the same
    		System.out.println(sf.reverse());
    	}
    }


    i use a literal of chars for my example but you can change easily to your param. and i used the StringBuilder only for testing purpose. good luck.