search
HomeJavaJavagetting StartedUse java to output a two-dimensional array clockwise

Use java to output a two-dimensional array clockwise

Apr 16, 2020 pm 04:11 PM
javaTwo-dimensional array

Use java to output a two-dimensional array clockwise

Goal:

Output a two-dimensional array clockwise.

(Recommended tutorial: Getting started with java)

The code is implemented as follows:

public class 顺时针输出二维矩阵 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[][]arr={
				 {1,2,2,3},
				 {4,5,4,6},
				 {7,8,6,9},
				 {1,2,3,4}
				 };
		int k=0;
		for(int i = 0; i < arr.length;i++){
			for(int j = 0;j < arr[0].length;j++){
				k++;
				System.out.print(arr[i][j]+" ");
				if(k % 4 == 0){
					System.out.println("");
				}
			}	
		}
		System.out.println("");
		System.out.println(arr[0].length);//列数
		
		System.out.println(arr.length);//行数	
		print(arr);
	}
	static void print (int[][]arr){
		int leftUpRow = 0;//最左行
		int leftUpCol = 0;//最左列
		int rightDownRow = arr.length-1;//最右列
		int rightDownCol = arr[0].length-1;//最右列
		while(leftUpRow <= rightDownRow && leftUpCol  <= rightDownCol){
			int row = leftUpRow, col = leftUpCol;
			//矩阵的第一行,此时行不变,列++
			while(col <= rightDownCol){
				System.out.print(arr[row][col++]+" ");
			}
			//矩阵右边第一列  此时行++,列不变
			//将 col,row 恢复
			col = rightDownCol;
			row++;
			while(row <= rightDownRow){
				System.out.print(arr[row++][col]+" ");
			}
			//矩阵的最下面一行  此时 行不变 列--
			//还需要恢复 col row 的值
			row = rightDownRow;
			col--;
			while(col >= leftUpCol){
				System.out.print(arr[row][col--]+" ");
			}
			//矩阵最左边一列,此时行--,列不变
			//继续恢复 col row的值
			col = leftUpCol;
			row--;
			while(row > leftUpRow){
				System.out.print(arr[row--][col]+" ");
			}
			leftUpRow++;
			leftUpCol++;
			rightDownRow--;
			rightDownCol--;
		
		}
	}
}

Output results:

1 2 2 3 
4 5 4 6 
7 8 6 9 
1 2 3 4 

1 2 2 3 6 9 4 3 2 1 7 4 5 4 6 8

Recommended related video tutorials :java视频

The above is the detailed content of Use java to output a two-dimensional array clockwise. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:csdn. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools