课程: Java Practice: Collections
Solution: Reverse order a list
- [Instructor] Here is a solution to this problem. So what we want to do is create a new list based on the source list, and then we're going to reverse that and return it. And actually, there's a built-in method in Java for reversing a list. So let's take a look at how that works. I'm going to create a list of integers, and we'll call this result, and we'll set it equal to a new array list. And in the parentheses of the constructor for the array list, we're going to pass in the source list. Then we'll use the built-in method, collections dot reverse passing in the result list and then we simply return the result list. I'll erase that extra comment in there and that's really all there is to it. We create a new array list based on the source, reverse it, which actually modifies the value you pass in there, and then we return the result. Hit the test my code button and you should see successful output in the console. You should also be able to modify any of these numbers that you want like so, hit test, and there it goes. So, one way to solve this problem is by calling the collections reverse static method.