Java陣列旋轉程式
陣列是一種線性資料結構,用於儲存一組具有相似資料型別的元素。它以順序方式儲存資料。一旦我們建立了一個數組,我們就無法更改其大小,也就是說,它可以儲存固定數量的元素。陣列具有廣泛的應用和用例。此外,我們可以在陣列上執行許多操作。本文將幫助您瞭解陣列的基礎知識,我們還將編寫Java程式來對陣列執行向右和向左旋轉操作。
Java陣列旋轉程式
首先,讓我們瞭解在陣列上下文中向右和向左旋轉的含義。
在陣列的向右旋轉中,我們只需將陣列的元素向右移動到指定的位數,反之亦然,在向左旋轉的情況下,如下例所示。
示例
宣告陣列的語法
Data_Type nameOfarray[]; // declaration Or, // declaration with size Data_Type nameOfarray[] = new Data_Type[sizeofarray];
我們可以在程式中使用上述任何語法。
在直接跳轉到程式之前,讓我們瞭解一下我們將要在示例程式中使用的名為“System.arraycopy()”的內建方法。
System.arraycopy()方法
java.lang.System.arraycopy()是Java System類的靜態方法,用於將源陣列從指定的索引複製到目標陣列的指定索引。
語法
System.arraycopy(srcArray, index1, destArray, index2, length);
這裡:
srcArray - 要複製的陣列。
index1 - 需要複製的源陣列的起始索引。
index2 - 元素將被複制到的目標陣列的起始索引。
destArray - 將複製元素的陣列。
length - 要複製的元素數量。
示例1
在下面的示例中,我們將陣列向左旋轉兩次,從第二個索引開始。
方法
首先,定義一個方法以及兩個引數,它們接受整數和索引作為引數。
在這個方法內部,建立一個臨時陣列來儲存旋轉後的元素。然後,使用“arraycopy()”方法將原始陣列從指定的索引複製到臨時陣列。
再次使用“arraycopy()”方法將原始陣列的剩餘元素複製到臨時陣列,以便元素向左移動。現在,再次使用“arraycopy()”方法將所有旋轉後的元素從臨時陣列複製到原始陣列。
在main()方法中,宣告並初始化一個整數陣列,然後初始化一個整數變數以指定要旋轉的索引。然後,呼叫使用者定義的方法來旋轉陣列。
import java.util.Arrays; public class ArrayRotationleft { // user-defined method to rotate the given array public static void rotateArray(int[] arr, int rotateBy) { int length = arr.length; // Calculate the effective rotation value int rotation = rotateBy % length; // Create a temporary array to store rotated elements int[] temp = new int[rotation]; // Copy elements till the rotateBy to the temporary array System.arraycopy(arr, 0, temp, 0, rotation); // Shift the remaining elements to the left System.arraycopy(arr, rotation, arr, 0, length - rotation); // Copy the rotated elements from the temporary array to the original array System.arraycopy(temp, 0, arr, length - rotation, rotation); } public static void main(String[] args) { // declaring and initializing an array int[] arr = { 5, 7, 89, 91, 34, 21, 11, 0 }; int rotateBy = 2; System.out.println("The original array is as follows: "); // for-each loop to print original array for(int print : arr) { System.out.print(print + " "); } System.out.println(); System.out.println("The array after left rotation: "); // calling the method to rotate the array rotateArray(arr, rotateBy); // for-each loop to print rotated array for(int print : arr) { System.out.print(print + " "); } } }
輸出
The original array is as follows: 5 7 89 91 34 21 11 0 The array after left rotation: 89 91 34 21 11 0 5 7
示例2
要執行向右旋轉,我們需要從要旋轉陣列的位置複製元素,而不是從第0個索引複製原始陣列。
import java.util.Arrays; public class ArrayRotationright { public static void main(String[] args) { // declaring and initializing an array int[] arr = { 5, 7, 89, 91, 34, 21, 11, 0 }; int rotateBy = 2; System.out.println("The original array is as follows: "); // for-each loop to print original array for(int print : arr) { System.out.print(print + " "); } System.out.println(); System.out.println("The array after right rotation: "); // calling the method to rotate the array rotateArray(arr, rotateBy); // for-each loop to print rotated array for(int print : arr) { System.out.print(print + " "); } } // user-defined method to rotate the given array public static void rotateArray(int[] arr, int rotateBy) { int length = arr.length; // Calculate the effective rotation value int rotation = rotateBy % length; // Create a temporary array to store rotated elements int[] temp = new int[rotation]; // Copy the elements till rotateBy to the temporary array System.arraycopy(arr, length - rotation, temp, 0, rotation); // Shift the remaining elements to the right System.arraycopy(arr, 0, arr, rotation, length - rotation); // Copy the rotated elements from the temporary array to the original array System.arraycopy(temp, 0, arr, 0, rotation); } }
輸出
The original array is as follows: 5 7 89 91 34 21 11 0 The array after right rotation: 11 0 5 7 89 91 34 21
結論
在本文中,我們瞭解了什麼是陣列,並討論了兩個用於執行陣列向右和向左旋轉的Java程式。此外,我們還發現了arraycopy()方法,它在將元素從一個數組複製到另一個數組時非常有用。