Why is it showing out of bound error

public static void print(int[][] arr) {

	  int top=0;
	  int bottom=arr.length-1;
	  int left=0;
	  int right= arr[top].length-1;
	  int dir=1;
	  int count=(bottom+1)*(right+1);
	  while(left<=right && top<=bottom) {
		  if(count>0) {
			  if(dir==1) {
				  for(int i=left;i<=right;i++) {
					  System.out.print(arr[top][i]);
					  count--;
				  }
				  top--;
				  dir=2;
			  }
		  }
		  if(count>0) {
			  if(dir==2){
				  for(int i=right;i>=left;i--) {
					  System.out.print(arr[top][i]);
					  count--;
				  }
				  top++;
				  dir=1;
			  }
		  }
	  }
}

public static void main(String[] args) {
	int[][] arr= {{11,12,13,14,15},{21,22,23,24,25},{31,32,33,34,35},{41,42,43,44,45},{51,52,53,54,55}};
	print(arr);
}

}

@laibaahsan27_1dfa992390072fd9 very sorry for the late reply Its bcoz my IDE was not working So I wasn’t able to check the code. Now Coming to your doubt :
Your code has Two problems :
i) dir == 1 then you need to do top++; not top-- bcoz bcoz you need to move to next line not the previous one.
ii)when dir == 2you need to do top-- not top++ bcoz you need to move to previous line here.
iii) A trivial problem is also there to distinguish between printing elements put a space also in Syso Statement like : System. out .print(arr[top][i]+" ");
Fully Corrected Code :

public static void print(int[][] arr) {

		int top = 0;
		int bottom = arr.length - 1;
		int left = 0;
		int right = arr[top].length - 1;
		int dir = 1;
		int count = (bottom + 1) * (right + 1);
		while (left <= right && top <= bottom) {
			if (count > 0) {
				if (dir == 1) {
					for (int i = left; i <= right; i++) {
//added space
						System.out.print(arr[top][i]+" ");
						count--;
					}
//change 1 in next line
					top++;
					dir = 2;
				}
			}
			if (count > 0) {
				if (dir == 2) {
					for (int i = right; i >= left; i--) {
//added space
						System.out.print(arr[top][i]+" ");
						count--;
					}
//Change 2 in next line
					top--;
					dir = 1;
				}
			}
		}
	}

	public static void main(String[] args) {
		int[][] arr = { { 11, 12, 13, 14, 15 }, { 21, 22, 23, 24, 25 }, { 31, 32, 33, 34, 35 }, { 41, 42, 43, 44, 45 },
				{ 51, 52, 53, 54, 55 } };
		print(arr);
	}