用java实现数组的循环逐步移位操作


package com.java.test;

import java.util.Arrays;

public class Test {
	
	public static void main(String[] args) {
		int[] array = new int[]{1,5,6,7,9,2,4};
		Test t = new Test();
		int i = 0 ;
		while (i < array.length - 1) {
			t.move(array);
			i++ ;
		}
	}
	
	public static void move(int[] array){
		int temp = array[array.length - 1] ;
		 for (int i = array.length - 1; i > 0; i--) {
			  array[i] = array[i-1];
		 }
		 array[0] = temp ;
		 System.out.println(Arrays.toString(array));
	}
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一个Java程序来实现循环方阵。以下是代码和解释: ```java import java.util.Arrays; public class CircularShiftMatrix { public static void main(String[] args) { int size = 4; // 方阵大小 int shift = 2; // 动位数 int[] matrix = new int[size * size]; // 一维数组储存方阵 // 初始化方阵 for (int i = 0; i < size * size; i++) { matrix[i] = i + 1; } // 按指定位方向和动位数动方阵 switch ("左") { // 指定位方向,这里为左 case "上": matrix = shiftUp(matrix, size, shift); break; case "下": matrix = shiftDown(matrix, size, shift); break; case "左": matrix = shiftLeft(matrix, size, shift); break; case "右": matrix = shiftRight(matrix, size, shift); break; } // 显示计算结果 for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { System.out.print(matrix[i * size + j] + "\t"); } System.out.println(); } } // 左 public static int[] shiftLeft(int[] matrix, int size, int shift) { int[] result = Arrays.copyOf(matrix, matrix.length); for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { int index = i * size + j; int row = index / size; int col = index % size; int newRow = row; int newCol = (col - shift + size) % size; result[newRow * size + newCol] = matrix[index]; } } return result; } // 右 public static int[] shiftRight(int[] matrix, int size, int shift) { int[] result = Arrays.copyOf(matrix, matrix.length); for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { int index = i * size + j; int row = index / size; int col = index % size; int newRow = row; int newCol = (col + shift) % size; result[newRow * size + newCol] = matrix[index]; } } return result; } // 上 public static int[] shiftUp(int[] matrix, int size, int shift) { int[] result = Arrays.copyOf(matrix, matrix.length); for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { int index = i * size + j; int row = index / size; int col = index % size; int newRow = (row - shift + size) % size; int newCol = col; result[newRow * size + newCol] = matrix[index]; } } return result; } // 下 public static int[] shiftDown(int[] matrix, int size, int shift) { int[] result = Arrays.copyOf(matrix, matrix.length); for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { int index = i * size + j; int row = index / size; int col = index % size; int newRow = (row + shift) % size; int newCol = col; result[newRow * size + newCol] = matrix[index]; } } return result; } } ``` 这个程序实现循环方阵的四个方向的动。您可以通过修改代码中的`size`和`shift`值来改变方阵大小和动位数,通过修改`switch`语句中的指定位方向来改变动方向。程序输出的是以表格形式显示的计算结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值