Please advise.
Screen output from the program (System.out)
In
Spain
Stays
Mainly
The
Rain
Input data (System.in)
6
2
The
Rain
In
Spain
Stays
Mainly
package com.codegym.task.task07.task0720;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
/*
Shuffled just in time
*/
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(reader.readLine());
int M = Integer.parseInt(reader.readLine());
ArrayList<String> list = new ArrayList<String>();
String s;
//write your code here
for (int i = 0; i< N; i++){
list.add(reader.readLine());
}
ArrayList<String> list2 = (ArrayList<String>)list.clone();
if (M <= N){
for (int i = 0; i < M; i++){
s = list.get(i);
list2.add(s);
list2.remove(0);
}
}
else{
System.out.println("Index Out of Bounds");
}
for(String str:list2){
System.out.println(str);
}
}
}