Correct output when printing the matrix, but gives wrong answer


for(int m=0;m<rowList.size();m++)
for(int n=0;n<res[m].length;n++)
System.out.print(res[m][n]+" ");
this gives me right answer for the test case everyone is talking about below. But when i test it it gives me wrong error.
My matrix prints [[1], [3,5], [2], [4], [6],[7],[8]] but the final output shows [[1],[2],[3],[4],[5],[6],[7],[8]]
I don’t know how the function is returning value in main function as i can’t see it. please someone help.