public static void main(String[] args) throws Exception {
// Read strings from the console and declare an ArrayList here
BufferedReader reader = new BufferedReader (new InputStreamReader(System.in));
ArrayList<String> list = new ArrayList<String>();
for(int i = 0 ; i<5;i++){
list.add(reader.readLine());
}
ArrayList<String> result = doubleValues(list);
// Display result
for(String sd : result){
System.out.println(sd);
}
}
public static ArrayList<String> doubleValues(ArrayList<String> list) {
//write your code here
ArrayList<String> reversal = new ArrayList<String>(reverse(list));
list.addAll(reversal);
/* for(int i = list.size()-1 ; i > -1; i--) {
list.add(i, list.get(i));
}
*/
return list;
}
}
why can't I write the using reverse method.
Under discussion
Comments (9)
- Popular
- New
- Old
You must be signed in to leave a comment
Jeanine Kimball
1 September 2020, 17:23
You only need to display it in reverse order. You don't need to save it that way.
0
Guadalupe Gagnon
1 September 2020, 17:01
there is no reverse method in this code.
0
Hari
1 September 2020, 17:26
ArrayList<String> reversal = new ArrayList<String>(reverse(list));
I want to use something like.
reverse(list) and store it and then use something like list.addall(reverse(list)/arraylist variable)
0
Hari
1 September 2020, 16:45
My questions is how can I use reverse method in collections to solve the above problem.
what I want to do is.
1. read the list.
2. reverse the list.
3. add the reversed list to the original list using addAll method.
I tried few things but I was not able to achieve it. Can some please help me in solving that way, as to how can I implement the idea I have if it is even right approach?!
0
Guadalupe Gagnon
1 September 2020, 17:34useful
When you know what you want to do do but not how to do it I always suggest google. I do this all the time personally because, while the Java language is small, the Java library is HUGE. You most likely will never know it all considering it continues expanding, not even including all the third party libraries that can enhance. Google is going to be your friend if you pursue this career:
https://www.geeksforgeeks.org/collections-reverse-java-examples/
- HOWEVER -
This task does not require you to use reverse. Reversing and then adding all will fail the task as the order will not be correct.
+1
Hari
1 September 2020, 18:06
Thanks.
I wonder how
example. 1,2,3
reverse order is 3,2,1
addall : 1,2,3,3,2,1 (this is what is required) right.
0
Guadalupe Gagnon
1 September 2020, 18:09
no, the requirements would be:
1,1,2,2,3,3
+1
Hari
1 September 2020, 18:14
My Bad. you are right!. Thanks
+1
Crawler_RG
1 September 2020, 16:40
Dont know, mb cuz u dont reverse anything.
0