This article describes the Java rotating two-dimensional array of operations, shared for everyone to reference. The implementation methods are as follows:
Copy Code code as follows:
Package test;
/*
* 1 2 3 4 5
* 16 17 18 19 6
* 15 24 25 20 7
* 14 23 22 21 8
* 13 12 11 10 9
*
* Write a method that prints a two-dimensional array of equal length, requiring that the natural number starting from 1 be arranged in the order of the outer ring of the phalanx in an inward spiral manner.
* */
public class Test6
{
public static void Main (string[] args)
{
Arraynum (4);
}
Easy to change code. Input different Y-values to output different two-dimensional sequences
private static void Arraynum (int x)
{
int[][] arr = new Int[x][x];
int len = arr.length, max = 0, count = 0;
Specarr (arr, Len, Max, Count);
Arrprint (arr);
}
Advanced for Output printing
private static void Arrprint (int[][] arr)
{
For (int[] in:arr)
{
for (int t:in)
{
System.out.print (t + "\ T");
}
System.out.println ();
}
}
private static void Specarr (int[][] arr, int len, int max, int count)
{
while (Len > 0)
{
int j = 0;
for (int index = 0; index < (len-1) * 4; index++)
{
if (Index < len-1)
Arr[0 + Count][index + count] = ++max;
else if (Index < 2 * (len-1))
Arr[count + j++][arr.length-1-count] = ++max;
else if (Index < 3 * (len-1))
Arr[arr.length-1-count][(j--) + count] = ++max;
else if (Index < 4 * (len-1))
Arr[arr.length-1-(j + +)-count][0 + count] = ++max;
}
if (len = = 1)
{
ARR[ARR.LENGTH/2][ARR.LENGTH/2] = max + 1;
}//noticed that when the Y value is odd, there is a loop to the n=1, and the middle value of the array needs to be filled in.
count++;
len = len-2;
}
}
}
I hope this article will help you with your Java programming.