www.webdeveloper.com
Results 1 to 2 of 2

Thread: Mysterious ArrayIndexOutOfBoundsException

  1. #1
    Join Date
    Feb 2012
    Posts
    1

    Mysterious ArrayIndexOutOfBoundsException

    I'm trying to call a method that prints the first and third quartiles of a data set (array of doubles), and to do that it needs to check whether or not the array of doubles has been sorted (using Array.sort() ). To check if the array has been sorted, I use static boolean isSorted(double[] data).

    Code:
     
          static boolean isSorted(double[] data)
          {
    		int biggerCount = 0;
    		for(int m = 0; m<data.length;m++)
    		{
    			if(data[m] != Math.min(data[m], data[m+1]) )
    				biggerCount++;
    			else{}
    		}
    		if(biggerCount == 0)
    			return true;
    		else
    			return false;
          }
    On the line containing the if-statement, I get the ArrayIndexOutOfBoundsException, when running the driver program. Any ideas as to how to fix this?

  2. #2
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,119
    problem has to be with the following part
    if(data[m] != Math.min(data[m], data[m+1])
    m+1 goes out of bounds. Look into it and make sure why you have to do m+1, if you must then you have check the array length so that it doesnt go beyond that such as
    if((m+1)<data.length && data[m] != Math.min(data[m], data[m+1])

    but u have to see what is your logic and why you are going beyond array limit
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles