```
public class Solution {
//This is not the best code and I'm just implementing it in a long way so that people who don't
//understand the concept can understand in a step by step manner..
//Run time of the program isn't gonna win any awards either.
public void rotate(int[][] matrix) {
if (matrix == null || matrix.length == 0) {
return;
}
int length = matrix.length;
int layer = length/2;
int count = 0;
while (count < layer) {
//Copy top elements
ArrayList<Integer> topList = new ArrayList<>();
for (int i=count; i<length-count; i++) {
topList.add(matrix[count][i]);
}
//Copy right elements
ArrayList<Integer> rightList = new ArrayList<>();
for (int i=count; i<length-count; i++) {
rightList.add(matrix[i][length-count-1]);
}
//Copy bottom elements
ArrayList<Integer> bottomList = new ArrayList<>();
for (int i=length-1-count; i>=count; i--) {
bottomList.add(matrix[length-1-count][i]);
}
//Copy left elements
ArrayList<Integer> leftList = new ArrayList<>();
for (int i=length-1-count; i>=count; i--) {
leftList.add(matrix[i][count]);
}
//Now copying the elements from top to the right
int index = 0;
for (int i=count; i<length-count && index < length; i++) {
matrix[i][length-count-1] = topList.get(index++);
}
//Now copying the elements from right to bottom
index = 0;
for (int i=length-1-count; i>=count && index < length; i--) {
matrix[length-1-count][i] = rightList.get(index++);
}
//Now copying the elements from bottom to left
index = 0;
for (int i=length-1-count; i>=count && index < length; i--) {
matrix[i][count] = bottomList.get(index++);
}
//Now copying the elements from left to top
index = 0;
for (int i=count; index < length && i<length-count; i++) {
matrix[count][i] = leftList.get(index++);
}
count++;
}
}
}
```