Java Error ArrayIndexOutOfBoundsException

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Rohit Rane
    New Member
    • Jan 2012
    • 5

    Java Error ArrayIndexOutOfBoundsException

    Code:
    class selection
    {
    	public static void sort(int arr[])
    	{
    		for(int i= arr.length;i>0;i++)
    		{
    			int m=0;
    			for(int j=1;j<=i;j++)
    			{
    				if(arr[j]>arr[m])
    				{
    					m=j;
    				}
    			}
    			int temp= arr[i];
    			arr[i]= arr[m];
    			arr[m]= temp;
    		}
    		System.out.println("Array After Sort : ");
    		System.out.print("{");
    		for(int i=0;i<arr.length;i++)
    			System.out.println(arr[i]+",");
    		System.out.print("}");
    	}
    	public static void main(String args[])
    	{
    		int arr[] = {11,55,13,5,3};
    		System.out.print("Array before sort : ");
    		System.out.print("{");
    		for(int i=0;i<arr.length;i++)
    			System.out.print(arr[i]+",");
    		System.out.println("}");
    		sort(arr);
    	}
    }
    Output :
    Array before sort : {11,55,13,5,3,}
    Excepion in thread "main" java.lang.Array IndexOutOfBound sException: 5
    at selection.sort( selection.java: 10)
    at selection.main( selection.java: 33)
    Last edited by Stewart Ross; Jan 29 '12, 08:33 PM. Reason: Added code tags to code segment
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    You're going out of bounds in the first for loop in the sort function.

    Comment

    • Rohit Rane
      New Member
      • Jan 2012
      • 5

      #3
      so what changes in code should i do

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        Well you should probably initialize i to 0 and the continue condition should be i < arr.length. But what you actually want to put in there is dependent on your business logic. You haven't explained what that is so I can't say what is correct. With the information you've given me, I can only tell you where it's wrong, not how to fix it.

        Comment

        • Rohit Rane
          New Member
          • Jan 2012
          • 5

          #5
          well this is part of data structure in java
          topic named is Selection sorting
          n tnx for replying u r answer will be well appretiated

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            It sounds like you're just doing a bubble sort. In which case you should change those values to the ones I listed in the fourth post.

            Comment

            • neeraj0708
              New Member
              • Feb 2012
              • 21

              #7
              hi,

              As array are "0 based index" and you are running the loop more than its length in first for loop. and incrementing the value of i.

              try this

              for(int i= arr.length-1;i>0;i--)

              Hope it will work fine.

              Comment

              Working...