From the course: Java Practice: Collections

Unlock this course with a free trial

Join today to access over 25,300 courses taught by industry experts.

Solution: Reverse order a list

Solution: Reverse order a list - Java Tutorial

From the course: 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.…

Contents