求二维数组对角钱
来源:互联网 发布:linux下的c ide 编辑:程序博客网 时间:2024/06/09 17:07
package com.test01;/**二维数组 对角线输出 两个方向例如对于数组:{ 1, 2, 3, 4 }, { 5, 6, 7, 8 },{ 9, 10, 11, 12 }, { 13, 14, 15, 16 },slash方向输出:1 5 2 9 6 3 13 10 7 4 14 11 8 15 12 16 backslash输出:4 3 8 2 7 12 1 6 11 16 5 10 15 9 14 13 数组的行数和列数可以不相等,但同一行的元素个数必须相等,称为"矩阵"更合适 */public class PrintArray { public static void main(String[] args) { int[][] arr = new int[][] { { 1, 2, 3, 4, }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 }, { 13, 14, 15, 16 }, }; slash(arr); System.out.println("======"); backslash(arr); } public static void slash(int[][] arr) { if (arr == null) { throw new IllegalArgumentException("arr cannot be null"); } int col = arr[0].length;//col列号 int row = arr.length;//row行号 for( int k = 0 ; k < col * 2 -1 ; k++ ) { //k代表有多少行输出,由col决定 int sum = k; //同一行的元素,行下标和列下标的和相等 for( int j = 0 ; j < col ; j++ ) { int i = sum - j; if(isValidIndex(i, row) && isValidIndex(j, col)) { System.out.print( arr[i][j] + " " ); } } System.out.println(); } } public static void backslash(int[][] arr) { if (arr == null) { throw new IllegalArgumentException("arr cannot be null"); } int row = arr.length; int col = arr[0].length; for (int k = 0; k < col * 2 - 1; k++) { //k代表有多少行输出,由col决定 int diff = col - k - 1; //同一行的元素,行下标和列下标的差相等 for (int j = 0; j < col; j++) { int i = j - diff; if (isValidIndex(i, row) && isValidIndex(j, col)) { System.out.print(arr[i][j] + " "); } } System.out.println(); } } private static boolean isValidIndex(int i, int n) { return i >= 0 && i < n; }}
转http://bylijinnan.iteye.com/blog/2056301
0 0
- 求二维数组对角钱
- FORTRAN 函数子程序 求二维数组主对角元素和
- 二维数组的相关操作,求主/斜对角,求外围数字和等
- 找出二维数组中某元素的所有对角元素
- 二维数组求最大值
- 二维数组求成绩
- 求二维数组最小值
- 二维数组求最大值
- 求二维数组鞍点
- Java平台 针对行列相等的二维数组非对角元素求法
- C语言基础 计算二维数组主对角线上的元素之和
- C语言基础 用行指针处理二维数组,计算主对角线上元素的和
- 二维数组计算4*4矩阵对角线上的元素之和
- 用二维数组求最小值
- 求二维数组中的最小值
- 求二维数组中的最大值
- 求二维数组的鞍点
- 求二维数组的鞍点
- oracle存储过程中is和as区别
- lib路径设置正确,lib文件名设置正确,链接时却出现莫名错误
- 求无序数组中第二大的数--快速选择
- SAP ABAP OLE 操作EXCEL小结
- Maven聚合
- 求二维数组对角钱
- 使用Eclipse+tomcat进行远程调试
- LayoutInflater作用及使用
- 如何正确的理解和解决 ORA-01843:not a valid month
- .net怎么给控件添加样式
- Inside Microsoft.NET IL Assembler中的Forward Declaration问题
- IOS Sent Events 事件
- java中如何知道一个字符串中有多少个字,把每个字打印出来,举例
- /dev/null文件使用