Java程序将数组循环旋转一个。
循环旋转数组的内容-
创建一个空变量。(温度)
将数组的最后一个元素保存在其中。
现在,从数组的第n个元素开始,将当前元素替换为前一个元素。
将元素存储在第一位置的temp中。
示例
import java.util.Arrays;
import java.util.Scanner;
public class CyclicallyRotateanArray {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the required size of the array ::");
int size = sc.nextInt();
int [] myArray = new int[size];
System.out.println("Enter elements of the array");
for(int i=0; i< size; i++){
myArray[i] = sc.nextInt();
}
System.out.println("Contents of the array: "+Arrays.toString(myArray));
int temp = myArray[size-1];
for(int i = size-1; i>0; i--){
myArray[i] = myArray[i-1];
}
myArray[0] = temp;
System.out.println("Contents of the cycled array: "+Arrays.toString(myArray));
}
}输出结果
Enter the required size of the array :: 5 Enter elements of the array 14 15 16 17 18 Contents of the array: [14, 15, 16, 17, 18] Contents of the cycled array: [18, 14, 15, 16, 17]
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短