Java-based spiral array and java-based spiral Array

Source: Internet
Author: User

Java-based spiral array and java-based spiral Array

The number of rows and columns of the received array, and returns the spiral array in the forward and reverse order.

Package cn. baokx; public class Test {public static void main (String [] args) {printArray (getSpiralArray (5, 5, false); System. out. println ("***************"); printArray (getSpiralArray (5, 5, true ));} // return the spiral array public static int [] [] getSpiralArray (int m, int n, boolean reverse) {// defines an array with a length of m * n, and initialize int [] numArray = new int [m * n] in sequence; for (int I = 0; I <numArray. length; I ++) {if (! Reverse) {numArray [I] = (I + 1);} else {numArray [I] = (numArray. length-I) ;}/// initialize the array subscript int foot = 0; // declare the spiral array int [] [] array = new int [m] [n]; // calculate the "number of layers". Take the smaller numbers in m and n as the standard. int layer = m <n? M: n; layer = (layer % 2 = 1 )? (Layer/2 + 1) :( layer/2); // loop from the outer layer to the inner layer for (int I = 0; I <layer; I ++) {// left to right for (int j = I; j <n-I; j ++) {array [I] [j] = numArray [foot ++]; if (foot> = m * n) {return array ;}// from top to bottom for (int j = I + 1; j <m-I; j ++) {array [j] [n-I-1] = numArray [foot ++]; if (foot> = m * n) {return array ;}} // from right to left for (int j = n-I-2; j> = I; j --) {array [m-I-1] [j] = numArray [foot ++]; if (foot> = m * n) {return array ;}} // from bottom to top for (int j = m-I-2; j> I; j --) {array [j] [I] = numArray [foot ++]; if (foot> = m * n) {return array ;}} return array ;}// print the two-dimensional array public static void printArray (int [] [] array) {for (int I = 0; I <array. length; I ++) {for (int j = 0; j <array [0]. length; j ++) {if (array [I] [j] <10) {System. out. print ("0");} System. out. print (array [I] [j] + "");} System. out. println ();}}}




How to use JAVA to output a spiral Array
How to use JAVA to implement a spiral Matrix

Import java. io. *; public class RingDemo {
Public static void main (String [] args ){
String strIn = "";
System. out. print ("Enter the number of columns in the matrix :");
InputStreamReader input = new InputStreamReader (System. in );
BufferedReader buff = new BufferedReader (input );
Try {
StrIn = buff. readLine ();
} Catch (IOException e ){
System. out. println (e. toString ());
}
Int int1 = Integer. parseInt (strIn );
Int n = int1;
System. out. println ("this is a spiral array of" + n + :");
Int intA = 1; // Initialization
Int [] [] array = new int [n] [n];
Int intB;
If (n % 2! = 0 ){
IntB = n/2 + 1; // Number of I cycles when the odd number
} Else
IntB = n/2; // The number of I cycles in an even number
For (int I = 0; I <intB; I ++) {// from external to internal loop
// Start from left to right
For (int j = I; j <n-I; j ++ ){
Array [I] [j] = intA;
IntA ++;
}
// Vertical from top to bottom
For (int k = I + 1; k <n-I; k ++ ){
Array [k] [n-I-1] = intA;
IntA ++;
}
// From right to left
For (int l = n-I-2; l> = I; l --){
Array [n-I-1] [l] = intA;
IntA ++;
}
// From bottom to top
For (int m = n-I-2; m> I; m --){
Array [m] [I] = intA;
IntA ++;
}
}
// Output Array
For (int I = 0; I <n; I ++ ){
For (int j = 0; j <n; j ++ ){
System. out. print (array [I] [j] + "");
}
System. out. println ();
}}
}... Remaining full text>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.