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

Thread: ArrayIndexOutOfBoundsException Yelp!

  1. #1
    Junior Member
    Join Date
    Mar 2019
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default ArrayIndexOutOfBoundsException Yelp!

    I just need another set of eyes, this is driving me crazy.

    I have a function that takes an array, and tries to find and remove duplicates by storing unique strings in the temp[]. The code produces an ArrayIndexOutOfBoundsException in the inner for loop. I am not too sure why the inner loop exceeds the array index.

        public static String[] removeDup(String[] aStrings){
     
            String[] temp = new String[aStrings.length];
     
            for(int i = 0; i <=temp.length-1;i++){
                temp[i] = "";
     
            }
     
            for(int i = 0; i < temp.length; i++){
     
               [B] for(int j = i+1; i<temp.length; j++){      
     
                    if(!aStrings[i].equals(temp[j]))  [/B]           
     
                        temp[j] = aStrings[i];
     
                }
            }
     
            return temp;
        }
    Last edited by Norm; March 12th, 2019 at 07:39 PM. Reason: Code tags fixed: uses [] not <>

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

    Default Re: ArrayIndexOutOfBoundsException Yelp!

    ArrayIndexOutOfBoundsException in the inner for loop
    Can you copy and paste here the full text of the error message showing the statement number and the value of the bad index?

    why the inner loop exceeds the array index.
    Add a print statement that prints out the value of the index so you can see what the code is doing.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Re: ArrayIndexOutOfBoundsException Yelp!

    What do you think will happen when the outter for loop is on it's last iteration... the inner for loop is going to start at an index 1 past the length of the array.
    Remember that indexing an array starts at 0

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

    Default Re: ArrayIndexOutOfBoundsException Yelp!

    Try changing the loop indexes' names. For example change i to srcIndx and j to trgtIndx and see if the code looks right and works with those names.
    If you don't understand my answer, don't ignore it, ask a question.