foreach遍历数组、数组的转置与方阵的迹
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遍历数组、数组的转置与方阵的迹的更多相关文章
- forEach遍历对象数组案例
<script> var users = [ {name:'name1',age:21}, {name:'name2',age:22}, {name:'name3',age:23} ]; ...
- PHP~foreach遍历名单数组~有必要多次观看练习
- Java foreach操作(遍历)数组
语法: 我们分别使用 for 和 foreach 语句来遍历数组 运行结果: 练习: import java.util.Arrays; public class HelloWorld { public ...
- foreach遍历数组
foreach遍历一维数组 <?php //PHP数组遍历:foreach //定义数组 $arr=array(1,2,3,4,5,6,7,8,9,10); //foreach循环 foreac ...
- php foreach 语法的遍历来源数组如果不是一个有效数组php会出现错误警告 Invalid argument supplied for foreach()
在php中,foreach语法的遍历来源数组如果不是一个有效数组,php会出现错误警告 Invalid argument supplied for foreach() ,但是很多时候这个数组是取自某些 ...
- js中三个对数组操作的函数 indexOf()方法 filter筛选 forEach遍历 map遍历
indexOf()方法 indexOf()方法返回在该数组中第一个找到的元素位置,如果它不存在则返回-1. 不使用indexOf时 var arr = ['apple','orange','pea ...
- Java使用foreach语句对数组成员遍历输出
/** * 本程序使用foreach语句对数组成员进行遍历输出 * @author Lei * @version 2018-7-23 */ public class ForeachDemo { pub ...
- 用数组指针遍历数组,FOR/FOREACH遍历数组
1. 用数组指针遍历一维数组 <?php header("Content-type:text/html;charset=utf-8"); /*用数组指针遍历一位数组的值*/ ...
- forEach遍历数组对象且去重
forEach遍历数组对象 var obj1 = [{ key: '01', value: '哈哈' }, { key: '02', value: '旺旺' }, { key: '03', value ...
随机推荐
- CentOS虚拟机和物理机共享文件夹实现
安装open-vm-tools: yum -y install open-vm-tools yum -y install open-vm-tools yum -y install open-vm ...
- 12. Integer to Roman C++
直接将各个数位上每个数所代表的罗马数字表示成字符串数组,然后提取出num的各位数,将对应的string相加 class Solution { public: string intToRoman(int ...
- [vjudge contest15(xjoi)] C - Berzerk
CodeForces - 787C Rick and Morty are playing their own version of Berzerk (which has nothing in comm ...
- java.lang.Exception: DEBUG STACK TRACE for PoolBackedDataSource.close().
java.lang.Exception: DEBUG STACK TRACE for PoolBackedDataSource.close(). java.lang.Exception: DEBUG ...
- 浅谈mysql中各种表空间(tablespaces)的概念
mysql中,会涉及到各种表空间的概念,虽然,很多方面这些概念和Oracle有相似性,但也有很多不同的地方,初学者很容易被这些概念弄的晕头转向,从而,混淆这些概念的区别和理解,下面,就简要介绍和说明一 ...
- [转]find+xargs+sed批量替换
写代码时经常遇到要把 .c 和 .h的文件中的某些内容全部替换的情况,用sourceinsight 进行全局的查找是一个方法,但是sourceinsight只能替换一个文件中的字符串,不能同时替换多 ...
- summary_20th,Nov 2018
一. 常量: 相对于变量,不改变的量 规定常量名全部大写(实际还是变量) 二:数值的运算符: 1. 算术运算符 + 和, - 减, *乘, / 除(浮点数结果) // 取整, ...
- linux因勿删或误操作导致登录界面异常,命令无法使用,显示/bin/bash:No such file or directory
一.故障现象 1.用secure CRT连接服务器时显示: /bin/bash:No such file or directory 翻译成中文是:没有此类文件或目录 2.直接登录服务器执行命令时显示: ...
- POJ 2243 Knight Moves(BFS)
POJ 2243 Knight Moves A friend of you is doing research on the Traveling Knight Problem (TKP) where ...
- 【Loadrunner基础知识】web_get_int_proterty
1.函数作用:记录http请求的响应信息 2.函数参数: HTTP_INFO_RETURN_CODE : http请求返回的状态码: HTTP_INFO_DOWNLOAD_SIZE : 返回页面下载的 ...