当前位置:网站首页>二维数组相关知识
二维数组相关知识
2022-07-27 23:01:00 【弱冠初心】
二维数组就是一个特殊的一维数组,其每一个元素都是一个一维数组,例如:
int[][] num = new int[3][4];
二维数组的动态初始化
type[][] typeName = new type[typeLength1][typeLength2];
type 可以为基本数据类型和复合数据类型,typeLength1 和 typeLength2 必须为正整数,typeLength1 为行数,typeLength2 为列数。
int[][] a = new int[2][3];
解析:
即数组的嵌套,数组里面的元素又是一个数组
多维数组的引用(以二维数组为例)
对二维数组中的每个元素,引用方式为 arrayName[index1][index2],例如:
nums[0][0] = 9;
二维数组的声明和分配空间合并写
数据类型[][] 数组名=new 数据类型[数组长度][数组长度]; 或者 数据类型[][] 数组名=new 数据类型[数组长度][];
int[][] nums = new int[5][3];
二维数组的声明、分配空间、赋值合并写
数据类型[][] 数组名 = new 数据类型[][]{ {数据1,数据2,...,数据n},{数据1,数据2,...,数据n},....,{数据1,数据2,...,数据n}}; 注意:new后面的两个[][]里都不能写数组长度
char[][] chars = new char[][]{ {'a','b','c'},{'c','d'},{'m'}};
double[][] nums3 = { {2.5,5.5},{11.5,55.5},{99.9,88.8},{70.5,50}};
二维数组的遍历
public static void main(String[] args) {
//双层for循环
int[][] sums={
{1,2},{3,4}};
for(int i = 0;i<sums.length;i++){
for(int j=0;j<sums[i].length;j++){
System.out.print(sums[i][j]+" ");
}
}
}二维数组的内存图
实例:
题目:已知有3个班级各5名学员,请使用二维数组计算各个班级的总成绩
public class TwoArrayDemo05 {
public static void main(String[] args) {
//声明一个长度为3的二维数组,此二维数组里面的元素都是长度为5的一维数组
double[][] scores = new double[3][5];
//创建Scanner类对象
Scanner sc = new Scanner(System.in);
//通过循环向数组中存入数据
for (int i = 0; i < scores.length; i++) {
System.out.println("--------------------第"+(i+1)+"个班--------------------");
for (int j = 0; j < scores[i].length; j++) {
System.out.println("请输入第"+(j+1)+"个学生的成绩:");
scores[i][j]=sc.nextDouble();
}
}
//数据存储完毕之后输出成绩,一个班的成绩在一行显示
for (int i = 0; i < scores.length; i++) {
System.out.print("第"+(i+1)+"个班级的学生成绩:");
for (int j = 0; j < scores[i].length; j++) {
System.out.print(scores[i][j]+" ");
}
System.out.println();
}
//统计成绩
int totalSum=0;
System.out.println("--------------------成绩统计--------------------");
for (int i = 0; i < scores.length; i++) {
int sum = 0;//这个sum要声明在外层for循环内,外层for循环每循环一次,就声明以sum变量用来统计班级总成绩
for (int j = 0; j < scores[i].length; j++) {
sum+=scores[i][j];
}
System.out.println((i+1)+"班总成绩:"+sum);
totalSum+=sum;
}
System.out.println("总成绩:"+totalSum);
}
边栏推荐
- EWM receiving ECC delivery note verification logic problem
- 力挺吴雄昂!Arm中国管理层发公开信:对莫须有的指控感到震惊和愤怒!
- 【STM32】看门狗模块
- 网络安全漏洞分析与漏洞复现
- Uni app advanced style framework / production environment
- Xinyi information technology, a domestic NB IOT chip manufacturer, received 200million yuan of a+ round financing
- Swoole websocket service
- Redis cache penetration breakdown and avalanche
- Starfish Os打造的元宇宙生态,跟MetaBell的合作只是开始
- Unknown database ‘xxxxx‘
猜你喜欢

Multithreading & high concurrency (the latest in the whole network: interview questions + map + Notes) the interviewer is calm

线性代数 【23】 概念的深入01 - Points坐标点和Vectors向量

Database daily question --- day 22: last login
![论文赏析[ICLR18]联合句法和词汇学习的神经语言模型](/img/1c/5b9726b16f67dfc2016a0c2035baae.png)
论文赏析[ICLR18]联合句法和词汇学习的神经语言模型

Iperf installation and use
![[STM32] watchdog module](/img/63/346d07c7febbaff69707f47ecb337c.png)
[STM32] watchdog module

Node red interacts with tdengine

网络安全漏洞分析与漏洞复现

Safety detection risk

Spool timer
随机推荐
网络安全漏洞分析与漏洞复现
Uni app advanced style framework / production environment
Swoole协程
Wavelet transform learning notes
BAT大厂测试架构师如何解读测试平台的各种争议
The cooperation between starfish OS and metabell is just the beginning
SAP各模块优缺点和发展简析
What is the reason for Chinese garbled code when dataworks transmits data to MySQL
Oracle error: ora-01722 invalid number
Redis-哨兵模式
One year anniversary of creation, Chongba young Lang
mysql查询条件字段值末尾有空格也能查到数据问题
闻泰科技收购安世半导体剩余股权获得无条件通过
Circular structure of shell system learning
ABAP CDS Table Function介绍与示例
Jointly create a new chapter in cultural tourism | xinqidian signs a strategic cooperation agreement with Guohua cultural tourism
总投资近16亿元!乾照光电VCSEL、高端LED芯片项目正式开工
接口测试实战项目02:读懂接口测试文档,上手操练
Brief analysis of advantages, disadvantages and development of SAP modules
【STM32】看门狗模块