课程: Java Algorithms

今天就学习课程吧!

今天就开通帐号,24,700 门业界名师课程任您挑!

Reverse an array in Java

Reverse an array in Java - Java教程

课程: Java Algorithms

Reverse an array in Java

- [Instructor] As a software developer, requirements can change and sometimes you'll need to change the ordering of your data. For example, you may need to swap elements or reverse your dataset entirely. Let's look at how to reverse an array in Java. One way is to copy all the contents into a new array but in reverse order. To start, we'd create a new array to hold the reversed contents. It would have the same length as the input array since no new contents are being added or removed, just rearranged. Then we'd iterate through the result and input array, and add the contents in reverse order. We'd start at zero and go to the input's length. This could also be the results length since both arrays are the same size. Then we'd set the value of result at index i equal to our input array at array.length - 1 - i. When i is zero, the first entry of the result array will be set to the last entry of the input array. When i is one, the second entry of the result array will be set to the…

内容