Print the matrix clockwise

Source: Internet
Author: User


Analysis:

Special case Processing

Code:
Package ARRAYS20; Public classDemo01 { Public Static void Clockwiseprintmatrix(int[] Arrays,intColumnsintrows) {if(arrays==NULL|| columns<=0|| rows<=0){return; }intStart =0; while(columns>start*2&& rows>start*2) {Circleprintmatrix (Arrays,columns,rows,start);        start++; }    } Public Static void Circleprintmatrix(int[] Arrays,intColumnsintRowsintStart) {intEndX = columns-1-start;intEndY = rows-1-start;//print a line from left to right         for(inti = start; I <= EndX; i++) {intNumber = Arrays[start][i]; System. out. Print (number+" "); }//print a line from top to bottom        if(Start<endy) { for(inti = start+1; I <= EndY; i++) {intNumber = Arrays[i][endx]; System. out. Print (number+" "); }        }//Print a line from right to left        if(Start<endx && Start<endy) { for(inti = endx-1; I >= start; i--) {intNumber = Arrays[endy][i]; System. out. Print (number+" "); }        }//Print one line from bottom to top (at least three rows and two columns)        if(Start<endx && start<endy-1) { for(inti = endy-1; I >= start+1; i--) {intNumber = Arrays[i][start]; System. out. Print (number+" "); }        }    } Public Static void Main(string[] args) {int[] Arrays = {{1,2,3,4},                {5,6,7,8},                {9,Ten, One, A},                { -, -, the, -}                };//system.out.println (arrays.length+ "," +arrays[0].length);        //Column linesClockwiseprintmatrix (arrays,arrays[0].length,arrays.length); }}

Operation Result:

Print the matrix clockwise

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.