public class Solution {
public static void main(String[] args) throws Exception {
// Read strings from the console and declare an ArrayList here
ArrayList<String> list =new ArrayList<String>();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
for(int i=0;i<10;i++){
String s=br.readLine();
list.add(s);
}
ArrayList<String> result = doubleValues(list);
// Display result
System.out.println(result);
}
public static ArrayList<String> doubleValues(ArrayList<String> list) {
//write your code here
for(int i=0;i<10;i++) {
list.add(i+1,list.get(i+1));
i++;
}
return list;
}
}
output: one , two, two, three, three, four, four, five, five, six, six, seven, eight, nine , ten
unable to understand how to print PLS HELP
Resolved
Comments (2)
- Popular
- New
- Old
You must be signed in to leave a comment
Guadalupe Gagnon
4 March 2019, 15:40useful
#1 each element needs to be duplicated, but your method misses the first one and the last three. Two problems here:
a) the loop should run not run as ling as 'i' is less than ten, but should run as long as i is less than the size
of the list.
b) you should be getting the value stored at 'i and not at 'i + 1'
So the code should look like this:
#2 From the task requirements:
"Display the resulting list, each element on a new line."
This is really easy to do, don't sys.out the result list like that, put it in a for loop that outputs result.get(i) like this:
+4
Mark Fernandez
4 March 2019, 16:04
Thanks GG you have been a great help. Since yesterday trying to figure out the solution.
+1