public class Copy1 {

     public static void main(String[] args) {
array1(); //如果不初始化元素,默认为0
int [][] a = new int [][]{{1,3,2,5,7},{5,9,3,4,2}};
int [][] b = new int [a[1].length][a.length];
for(int i=0;i<b.length;i++){ //数组的转置
for(int j =0;j<b[i].length;j++){
b[i][j]=a[j][i];
}
}
printArray(a);    //调用方法遍历a
System.out.println();
System.out.println();
printArray(b);                    //遍历b
System.out.println();
for(int []n:b){   //foreach用法,int[]n为零时变量
for(int m:n){     //int m零时变量
System.out.print(m+" ");
}
System.out.println();
}
System.out.println(); int [][] c=new int[][]{{1,2,3},{4,5,6},{7,8,9}}; //创建数组
printArray(c); //调用方法遍历数组
System.out.println();
System.out.println();
int [][] d=new int[c[0].length][c.length]; //d与c行列转换(d叫做转置后的c)
for(int i=0;i<d.length;i++){ //遍历
for(int j=0;j<d[i].length;j++){
d[i][j]=c[j][i];
System.out.print(d[i][j]+" ");
}
System.out.println();
}
System.out.println();
System.out.println(); //在方阵数组中d[i][j],当i==j时的所有元素的和叫做方阵的迹
{
int n=0;
for(int i=0;i<d.length;i++){
for(int j=0;j<d[i].length;j++){
if(i==j){
n+=d[i][j]; //当i==j时
}
}
}
System.out.println(n);
}
} private static void printArray(int[][] a) { //封装遍历数组方法
for(int i=0;i<a.length;i++){
for(int j=0;j<a[i].length;j++){
System.out.print(a[i][j]+" ");
}
System.out.println();
} } private static void array1() { //默认初始化元素为0
int [][] a= new int[3][4];
for(int i=0;i<a.length;i++){
for(int j=0;j<a[i].length;j++){
System.out.print(a[i][j]+" ");
}
System.out.println();
}
} }

foreach遍历数组、数组的转置与方阵的迹的更多相关文章

  1. forEach遍历对象数组案例

    <script> var users = [ {name:'name1',age:21}, {name:'name2',age:22}, {name:'name3',age:23} ]; ...

  2. PHP~foreach遍历名单数组~有必要多次观看练习

  3. Java foreach操作(遍历)数组

    语法: 我们分别使用 for 和 foreach 语句来遍历数组 运行结果: 练习: import java.util.Arrays; public class HelloWorld { public ...

  4. foreach遍历数组

    foreach遍历一维数组 <?php //PHP数组遍历:foreach //定义数组 $arr=array(1,2,3,4,5,6,7,8,9,10); //foreach循环 foreac ...

  5. php foreach 语法的遍历来源数组如果不是一个有效数组php会出现错误警告 Invalid argument supplied for foreach()

    在php中,foreach语法的遍历来源数组如果不是一个有效数组,php会出现错误警告 Invalid argument supplied for foreach() ,但是很多时候这个数组是取自某些 ...

  6. js中三个对数组操作的函数 indexOf()方法 filter筛选 forEach遍历 map遍历

     indexOf()方法  indexOf()方法返回在该数组中第一个找到的元素位置,如果它不存在则返回-1. 不使用indexOf时 var arr = ['apple','orange','pea ...

  7. Java使用foreach语句对数组成员遍历输出

    /** * 本程序使用foreach语句对数组成员进行遍历输出 * @author Lei * @version 2018-7-23 */ public class ForeachDemo { pub ...

  8. 用数组指针遍历数组,FOR/FOREACH遍历数组

    1. 用数组指针遍历一维数组 <?php header("Content-type:text/html;charset=utf-8"); /*用数组指针遍历一位数组的值*/ ...

  9. forEach遍历数组对象且去重

    forEach遍历数组对象 var obj1 = [{ key: '01', value: '哈哈' }, { key: '02', value: '旺旺' }, { key: '03', value ...

随机推荐

  1. CentOS虚拟机和物理机共享文件夹实现

    安装open-vm-tools: yum -y install  open-vm-tools yum -y  install open-vm-tools yum -y install  open-vm ...

  2. 12. Integer to Roman C++

    直接将各个数位上每个数所代表的罗马数字表示成字符串数组,然后提取出num的各位数,将对应的string相加 class Solution { public: string intToRoman(int ...

  3. [vjudge contest15(xjoi)] C - Berzerk

    CodeForces - 787C Rick and Morty are playing their own version of Berzerk (which has nothing in comm ...

  4. java.lang.Exception: DEBUG STACK TRACE for PoolBackedDataSource.close().

    java.lang.Exception: DEBUG STACK TRACE for PoolBackedDataSource.close(). java.lang.Exception: DEBUG ...

  5. 浅谈mysql中各种表空间(tablespaces)的概念

    mysql中,会涉及到各种表空间的概念,虽然,很多方面这些概念和Oracle有相似性,但也有很多不同的地方,初学者很容易被这些概念弄的晕头转向,从而,混淆这些概念的区别和理解,下面,就简要介绍和说明一 ...

  6. [转]find+xargs+sed批量替换

    写代码时经常遇到要把 .c  和 .h的文件中的某些内容全部替换的情况,用sourceinsight 进行全局的查找是一个方法,但是sourceinsight只能替换一个文件中的字符串,不能同时替换多 ...

  7. summary_20th,Nov 2018

    一. 常量: 相对于变量,不改变的量 规定常量名全部大写(实际还是变量) 二:数值的运算符: 1. 算术运算符 +  和,    - 减,    *乘,       / 除(浮点数结果) // 取整, ...

  8. linux因勿删或误操作导致登录界面异常,命令无法使用,显示/bin/bash:No such file or directory

    一.故障现象 1.用secure CRT连接服务器时显示: /bin/bash:No such file or directory 翻译成中文是:没有此类文件或目录 2.直接登录服务器执行命令时显示: ...

  9. POJ 2243 Knight Moves(BFS)

    POJ 2243 Knight Moves A friend of you is doing research on the Traveling Knight Problem (TKP) where ...

  10. 【Loadrunner基础知识】web_get_int_proterty

    1.函数作用:记录http请求的响应信息 2.函数参数: HTTP_INFO_RETURN_CODE : http请求返回的状态码: HTTP_INFO_DOWNLOAD_SIZE : 返回页面下载的 ...