二维数组螺旋输出例如:输入数组12345678910111213141516输出数组12341213145111615610987Java实现方案ArrClass.java/***二维数组的元素进行螺旋排列,二维数组是有序的,输出螺旋排序*@authorzdd*/publicclassArrClass{privateintl,r,u,d;//左右上下privateintn,m;//行列privateinti,j;//当前走到的位置;privateint[][]arr;//结果集privateenumDirection{//枚举right,down,left,up;}privateDirectionpos;privatevoidinit(intn,intm){this.n=n;this.m=m;this.l=-1;//左边界this.r=m;//右边界this.u=0;//上边界this.d=n;//下边界this.i=0;this.j=0;this.arr=newint[n][m];this.pos=Direction.right;//初始向右走}/***处理,螺旋排序*@paramarr二维数组,每行的列数必须相同;*@paramn参数arr的行数*@paramm参数arr的列数*/publicvoiddeal(Integer[][]arr,intn,intm){this.init(n,m);for(inti=0;in;i++){for(intj=0;jm;j++){intd=arr[i][j];booleanflag=push(d);while(!flag){flag=push(d);}}}}privatebooleanpush(intdata){if(pos==Direction.right){//向右if(jr){arr[i][j]=data;j++;returntrue;}else{j--;i++;r--;pos=Direction.down;returnfalse;}}elseif(pos==Direction.down){//向下if(id){arr[i][j]=data;i++;returntrue;}else{i--;j--;d--;pos=Direction.left;returnfalse;}}elseif(pos==Direction.left){//向左if(jl){arr[i][j]=data;j--;returntrue;}else{j++;i--;l++;pos=Direction.up;returnfalse;}}else{//向上if(iu){arr[i][j]=data;i--;returntrue;}else{i++;j++;u++;pos=Direction.right;returnfalse;}}}/***矩阵形式输出,空格分割开;*/publicvoidprint(){for(inti=0;in;i++){for(intj=0;jm;j++){System.out.print(arr[i][j]);if(jm-1){System.out.print();}}if(in-1){System.out.println();}}}/***get方法,获取结果集;*@returnarr处理后的二维数组;*/publicint[][]getArr(){returnarr;}}//二维数组的元素进行螺旋排列,二维数组是有序的,螺旋排序如表所示//输入:将从一个整形T开始,表示后续有T个实例。//每个实例第一行的两个整数NM表示二维数组行列数,//表示有N行数每行M个数构成数组,数组都是已经排序好的(排序数组)。//输出:打印出螺旋排序后的数组////样例输入://1//44//1234//5678//9101112//13141516//样例输出://1234//1213145//1116156//10987测试用例:Main.javaimportjava.util.Scanner;publicclassMain{publicstaticvoidmain(String[]args){Scannerscanner=newScanner(System.in);intnum=scanner.nextInt();intt=0;ArrClassac=newArrClass();while(tnum){Integern=scanner.nextInt();Integerm=scanner.nextInt();Integer[][]arr=newInteger[n][m];for(inti=0;in;i++){for(intj=0;jm;j++){arr[i][j]=scanner.nextInt();}}ac.deal(arr,n,m);ac.print();t++;if(tnum){System.out.println();}}}/*2441234567891011121314151643123456789101112*/}