there is problem with remove statement but i can't able to find out. please help
package com.codegym.task.task07.task0720;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
/*
Shuffled just in time
*/
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
ArrayList<String> list = new ArrayList<String>();
int N = Integer.parseInt(reader.readLine());
int M = Integer.parseInt(reader.readLine());
String[] list1 = new String[M];
for(int i = 0; i < N; i++){
String s = reader.readLine();
list.add(s);
}
for(int i = 0; i < M; i++){
list1[i] = list.get(i);
}
for (int x = 0;x < M; x++){
list.remove(x);
}
for(int i = 0; i < M; i++){
list.add(list1[i]);
}
for(String s : list){
System.out.println(s);
}
//write your code here
}
}