java.lang.ArrayIndexOutOfBoundsException in Java

In this program i going to display Two dimensional Array in Java.

Error Message:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
       at com.lib.TwoDimArray.main(TwoDimArray.java:12)


Error Description:

In for loops I placed lessthan or equalto so i got an error.

package com.lib;

public class TwoDimArray
{
       public static void main(String[] args)
       {
              int tr [][]= {{9,8,7},{2,6,4},{5,2,9}};
              for(int k=0;k<=3;k++)
              {
                     for(int l=0;l<=3;l++)
                     {
                            System.out.println(tr[k][l]+" ");
                     }
              }

       }

}


Error Solution:

package com.lib;

public class TwoDimArray
{
       public static void main(String[] args)
       {
              int tr[][]= {{9,8,7},{2,6,4},{5,2,9}};
              for(int k=0;k<3;k++)
              {
                     for(int l=0;l<3;l++)
                     {
                           System.out.print(tr[k][l]+" ");
                     }
                     System.out.println();
              }

       }

}


Output:
9 8 7
2 6 4
5 2 9 
Share this post :

Post a comment

Please share your valuable feedback and share this article in social media.

 
Support : Ranga Rajesh Kumar
Copyright © 2019. Error Solution - All Rights Reserved
Site Designed by Ranga Rajesh Kumar