BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
ArrayList<String> l = new ArrayList<String>();
for(int i=0; i<10; i++)
{
String ss = b.readLine();
//int size;
//add an element on index 0 in the list L
l.add(0, ss);
//logic assumption: if value has been added then it size become 1.
if (l.size()>=1)
{
//logic assumption: now pushing the value to next element i.e 1 for i=0
l.add(i+1,ss);
//logic assumption: removing the previous value stored at index 0;
l.remove(0);
}
}
for(int i=0; i<l.size(); i++)
{
System.out.println(l.get(i));
}
package com.codegym.task.task07.task0710;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
/*
To the top of the list
*/
public class Solution {
public static void main(String[] args) throws Exception {
//write your code here
//write your code here
BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
ArrayList<String> l = new ArrayList<String>();
for(int i=0; i<10; i++)
{
String ss = b.readLine();
//int size;
//add an element on index 0 in the list L
l.add(0, ss);
//logic assumption: if value has been added then it size become 1.
if (l.size()>=1)
{
//logic assumption:
l.add(i+=1,ss);
l.remove(0);
}
}
for(int i=0; i<l.size(); i++)
{
System.out.println(l.get(i));
}
}
}