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. jQuery生成二维码 jquery.qrcode.js

    https://github.com/jeromeetienne/jquery-qrcode 1.将jquery.qrcode.min.js和jquery添加到您的网页中 <script src ...

  2. bootstrap居中

    1.页面 <div class="container"> <div class="row clearfix"> <div clas ...

  3. git拉取远程分支并创建本地分支

    本地分支推送至远程 git checkout local_branch git push origin local_branch:remote_branch 一.查看远程分支 使用如下Git命令查看所 ...

  4. WebSphere概要文件的创建与删除

    一.创建单server服务器 /was/bin/manageprofiles.sh -create -profileName server1 \ -profilePath /was/profiles/ ...

  5. jquery attr方法获取input的checked属性问题

    1.通过prop方法获取checked属性,获取的checked返回值为boolean,选中为true,否则为flase <input type="checkbox" id= ...

  6. net core 上传并使用EPPlus导入Excel文件

    1.  cshtml页面 form <form id="form" method="post" action="/SaveValueBatch& ...

  7. maven运行时的配置及命令详解

      上面是指定端口运行程序的,也可以先指定好,直接在上面的地方写jettty:run           当然,如果你是在控制台运行且安装了maven,直接可以进入项目的文件中:mvn jetty:r ...

  8. vue 给 图片添加一个默认图片

    <img v-bind:src="userData.photo" :onerror="logo" class="img-box4"&g ...

  9. tp框架 php ajax 登陆

    html 文件 <form class="form-signin loginform" role="form"> <input type=&q ...

  10. sql取大的一个值

    select b.*,             a.recid,             a.keyno  from product b,             (select pcode,     ...