Java程序用于数组的右旋转的逆序算法

Array is a linear data structure that is used to store group of elements with similar datatypes. It stores data in a sequential manner. Once we create an array we can’t change its size i.e. it can store fixed number of elements.

本文将帮助您了解反转算法,并且我们将创建一个Java程序,在其中创建一个数组,并通过应用反转算法进行向右旋转。

数组的右旋转

让我们在数组的上下文中理解“右旋转”这个术语。

In right rotation of an array, we simply shift the elements of the array to our right till the specified number of rotations.

Example 1

Java程序用于数组的右旋转的逆序算法

Example 2

Java程序用于数组的右旋转的逆序算法

在上面的例子中,当我们将数组旋转2次时,从第0个位置开始的元素会被移动到第2个位置及以后的位置,而最后2个元素则被填充到前两个位置。

当我们将数组旋转4次时,从第0个位置开始的元素会被移动到第4个位置及以后。

声明数组的语法

Data_Type nameOfarray[]; // declaration Or, // declaration with size Data_Type nameOfarray[] = new Data_Type[sizeofarray]; 登录后复制