package com.codegym.task.task07.task0720;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
/*
Shuffled just in time
Use the keyboard to enter 2 numbers N and M.
Enter N strings and put them in a list.
Move the first M strings to the end of the list.
Display the list, each value on a new line.
*/
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader a = new BufferedReader(new InputStreamReader(System.in));
ArrayList<String> results = new ArrayList<>();
int N = Integer.parseInt(a.readLine());
int M = Integer.parseInt(a.readLine());
//Populate the list
for (int i = 0; i < N; i++) {
results.add(a.readLine());
}
//Copy the first M numbers to the end of the list
for (int i = 0; i < M; i++) {
results.add(results.get(i));
}
//Remove the first M numbers that are now redundant
for (int i = 0; i < M; i++) {
results.remove(i);
i++;
}
//Display the list
for (String s : results) {
System.out.println(s);
}
}
}
Not sure what the problem is
Resolved
Comments (8)
- Popular
- New
- Old
You must be signed in to leave a comment
vikr@nt
14 February 2019, 01:46
haha
You dont have to run loop twice. It can be done via this way
for (int i=0; i<M; i++){
list.add(list.get(0));
list.remove(0);
}
+4
hacks patel
22 December 2018, 06:44
if (N > M) {
String[] r = new String[N];
for (int i = 0; i < N; i++) {
list.add(reader.readLine());
}
for (int i = 0; i < M; i++) {
String s = list.get(i);
r[i] = s;
}
for (int i = 0; i < M; i++) {
list.add(r[i]);
}
for(int i = 0 ; i < M ; i++)
{
list.remove(0);
}
}
0
Michael Martin
13 November 2018, 02:15solution
What output do you get when you run without verifying?
+2
Juan Gallardo
14 November 2018, 02:47
Huh. I thought it was right. Fixed the removal part like so:
+2
Michael Martin
14 November 2018, 14:08
Is the second requirement now passing? (reading the numbers and adding to the list)
0
Michael Martin
14 November 2018, 14:09
The other problem you are having is you cannot add or remove from a list through a loop like that. You have to use an Iterator. But work on the input requirement first.
0
Juan Gallardo
15 November 2018, 12:34
Everything is passing now. I didn't have any problems removing through a for() loop
0
Michael Martin
15 November 2018, 19:04
Great!
0