Проблема с вашим кодом заключается в том, что вы инициализируете размер массива как 100, и когда позже, когда вы пытаетесь заменить последний элемент на arr1[i][arr1.length-1]=p;
, он заменяет последний 99-й индекс, а не 4-й индекс. Если вы будете выполнять итерацию по всему массиву, вы увидите, что эти значения наконец-то находятся. Мое предложение инициализировать массив с размером вашей потребности. import java .util.Scanner;
public class RotationOfAnArray {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
//initializing the variables and the matrix
int i,j,n,k,l,f,p;
//taking the input
n=sc.nextInt();
int[][] arr1;
for(i=0;i<n;i++){
k=sc.nextInt();
//********Initialise array here instead.******
arr1=new int[k][k];
l=sc.nextInt();
for(j=0;j<k;j++){
arr1[i][j]=sc.nextInt();
}
//in the above section taking the input of the elements of the array of the matrix
for(f=0;f<l;f++) {
p=arr1[0][0];
for(j=0;j<arr1.length-1;j++) {
arr1[i][j]=arr1[i][j+1];
}
//here the row of the particular matrix is not rotated
arr1[i][arr1.length-1]=p;
}
for(j=0;j<k;j++) {
System.out.print(arr1[0][j]+" ");
}
}
}
}