Results 1 to 6 of 6
  1. #1
    iamsethi is offline Member
    Join Date
    Dec 2014
    Posts
    19
    Rep Power
    0

    Angry Array Index Out of Bound Exception

    When i am trying to execute this code it is showing Array Index Out of Bound Exception-sort2d.png
    Java Code:
    class Sort2D
    {
    
    public static void main(String...s)
    		{
    		Sort2D r=new Sort2D();
    		int z[][]=r.sort(new int[][]{{1,0,567,4,3,33},{333,677,34243,8987,3434,324},{446,876,23,546565,332}});
    		for(int i=0;i<z.length;i++)
    		for(int j=0;j<z[i].length;j++)
    		System.out.println(z[i][j]);
    		}
    
    	int[][] sort(int x[][])
    		{int temp;
    		for(int i=0;i<x.length;i++)
    			{
    			for(int j=0;j<(x[i].length)-i;j++)
    				{
    				if(x[i][j]<x[i][j-1])
    					{
    					temp=x[i][j];
    					x[i][j]=x[i][j-1];
    					x[i][j-1]=temp;
    					}
    				}
    			}
    		return x;
    		}
    }
    Last edited by iamsethi; 12-15-2014 at 01:09 PM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Array Index Out of Bound Exception

    Please copy the full text of the error message and paste it here. It has important info about the error.

    Also please edit your post and wrap the code in code tags.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Array Index Out of Bound Exception

    If j equals zero, j-1 equals minus one ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  4. #4
    iamsethi is offline Member
    Join Date
    Dec 2014
    Posts
    19
    Rep Power
    0

    Default Re: Array Index Out of Bound Exception

    wow thanks! a lot.it worked

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Array Index Out of Bound Exception

    Quote Originally Posted by iamsethi View Post
    wow thanks! a lot.it worked
    It would be nice if you showed your solution to your problem to the other readers of this thread.

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  6. #6
    iamsethi is offline Member
    Join Date
    Dec 2014
    Posts
    19
    Rep Power
    0

    Default Re: Array Index Out of Bound Exception

    Correct Code:
    Java Code:
    class Sort2D
    {
     
    public static void main(String...s)
            {
            Sort2D r=new Sort2D();
            int z[][]=r.sort(new int[][]{{1,0,567,4,3,33},{333,677,34243,8987,3434,324},{446,876,23,546565,332}});
            for(int i=0;i<z.length;i++)
            for(int j=0;j<z[i].length;j++)
            System.out.println(z[i][j]);
            }
     
        int[][] sort(int x[][])
            {int temp;
            for(int i=0;i<x.length;i++)
                {
                for(int j=1;j<(x[i].length)-i;j++)
                    {
                    if(x[i][j]<x[i][j-1])
                        {
                        temp=x[i][j];
                        x[i][j]=x[i][j-1];
                        x[i][j-1]=temp;
                        }
                    }
                }
            return x;
            }
    }

Similar Threads

  1. Index out of bound exception
    By sh4rif in forum New To Java
    Replies: 7
    Last Post: 12-07-2011, 12:32 PM
  2. Replies: 5
    Last Post: 11-22-2011, 12:42 PM
  3. Array index out of bound exception error
    By rahulkrishnanr in forum Threads and Synchronization
    Replies: 7
    Last Post: 10-12-2010, 05:57 PM
  4. Array index Out of bound Exception
    By nitin_daviet88 in forum New To Java
    Replies: 9
    Last Post: 07-28-2010, 05:32 AM
  5. Array Index Out of bound exception
    By abhijit in forum Networking
    Replies: 7
    Last Post: 09-25-2009, 07:25 PM

Posting Permissions

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