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 11 of 11

Thread: ArrayOutOfBound exception

  1. #1
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default ArrayOutOfBound exception

    So I cant for the life of me figure out why I am getting this OutOfBound exception and why the program wont work.

    public class test {
    	public static void main(String[] args) {
     
    		int[] array = {58,24,13,15,63,9,8,81,1,78};
     
    		Out.println(java.util.Arrays.toString(array));
     
    		int[] firstArray= new int[array.length/2];
     
    		for(int i = 0; i<array.length/2; i++) {
    			firstArray[i] = array[i];
    		}
    		Out.println(java.util.Arrays.toString(firstArray));
     
    		int[] secondArray = new int[array.length/2];
     
    		for(int i = array.length/2; i<array.length-1; i++) {
    			secondArray[i] = array[i];
    		}
     
     
     
    		Out.println(java.util.Arrays.toString(secondArray));
    	}
    }

    So my goal here is to split this inital array in half and store both of the halves in new arrays.The first half works but the second I cant get to work. When I try to run the program it gives me "Index 5 out of bound for length 5 exception". Now the way I am seeing this is that the second array has the length of 5 and in the for loop we are starting from the 5th position going to the last element in the original array, and copying the values. Why am I getting this error?

    PS.I have also tried going backwards, trying to go from the last unit of the array to the middle and i get the same error

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ArrayOutOfBound exception

    "Index 5 out of bound for length 5 exception"
    An array with 5 elements has valid indexes from 0 to 4. An index of 5 is past the end of the array.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: ArrayOutOfBound exception

    Yea I get that but what if I start from index 0 i will just copy the first part of the array than.I've gotten the program to kinda work now, meaning if I put the length of the array to be 10, I get 5 zeros, followed by 9 8.. etc. How do I get rid of the zeros?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ArrayOutOfBound exception

    What do you mean by "get rid of the zeros"? Every slot in an int array will have a value, default is 0. If you don't want the end of the array to be 0s, then make the array the exact size to hold the data.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: ArrayOutOfBound exception

    Well I've tried making the array the exact size (array.length/2) but than I get the OutOfBound exception. Is the condition of my while loop wrong?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ArrayOutOfBound exception

    What is the size of the array? Does your index go past the end of the array?

    Can you copy the contents of the stack trace that shows what statement the exception was on and paste it here?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: ArrayOutOfBound exception

    [58, 24, 13, 15, 63, 9, 8, 81, 1, 78]
    [58, 24, 13, 15, 63]
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5
    at test.main(test.java:18)

    C:\Users\Hp\Desktop\test>

    So, the size of the new array is 5, when I print out the array.length/2 it gives out 5. Also when I print out an empty array of lenght/2 it gives out 5 zeros so the array is of length 5.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ArrayOutOfBound exception

    Is this the assignment statement at line 18?
                  for(int i = array.length/2; i<array.length-1; i++) {
    			secondArray[i] = array[i];
    Do the two arrays both have a slot at the index of array.length/2?
    Is secondArray the same length as array? That is needed for the indexes into secondArray to stay in bounds.

    Take a paper and pencil and draw the two arrays and the indexes into each for copying from array to secondArray.
    Are the indexes into the two array different? One starts at 0 and one at the middle of the array.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: ArrayOutOfBound exception

    Yea you were right, I've figure it out.

     
    public class test {
        public static void main(String[] args) {
     
            int[] array = {58,24,13,15,63,9,8,81,1,78};
     
            Out.println(java.util.Arrays.toString(array));
     
            int[] firstArray= new int[array.length/2];
     
            for(int i = 0; i<array.length/2; i++) {
                firstArray[i] = array[i];
            }
            Out.println(java.util.Arrays.toString(firstArray));
     
            int[] secondArray = new int[array.length/2];
     
            for(int i = array.length/2; i<array.length; i++) {
                secondArray[i - 5] = array[i];
            }
     
            Out.println(java.util.Arrays.toString(secondArray));
        }
    }
    Thank you !

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ArrayOutOfBound exception

    The -5 only works for an array with 10 elements. What if the initial array has 8 or 12 elements?
    Change the -5 to be a value derived from the size of the array.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: ArrayOutOfBound exception

    Yea ur right..,I'll look into it.

Similar Threads

  1. Exception in my code:Exception in thread "main" java.lang.NullPointerException
    By vickychawla.chawla in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 25th, 2014, 05:30 AM
  2. Replies: 4
    Last Post: February 20th, 2013, 10:01 AM
  3. Replies: 1
    Last Post: February 6th, 2013, 11:21 AM
  4. Replies: 5
    Last Post: September 5th, 2011, 10:31 AM
  5. Replies: 6
    Last Post: March 25th, 2011, 03:42 PM