当前位置:网站首页>二维数组及操作
二维数组及操作
2022-07-28 06:22:00 【抬眼远望】
多维数组
三维及以上的数组很少使用
主要使用二维数组
从语法上Java支持多维数组
从内存分配原理的角度讲,只有一维数组
二维数组
二维数组实际上是一个以一维数组做为元素的一维数组
二维数组:即数组的嵌套,数组里面的元素又是一个数组
1) 声明二维数组: 数据类型[][] 数组名; 或者 数据类型 数组名[][];
//声明一个int类型的二维数组
int[] [] nums;
//声明一个String类型的二维数组
String [] [] strs;2)分配空间(告诉你这个数组里可以存储多少个元素):
数组名 = new 数据类型[数组长度][数组长度]; 或者 数组名 = new 数据类型[数组长度][];
注意:
二维数组在分配空间的时候,第一个数组长度必须要写,告诉你这个二维数组里有多少个元素,第二数组长度可以写,可以不写,不过写了,表示这个二维数组里的里面的元素(一维数组)的长度,如果没有写,这个二维数组里面的元素(一维数组)长度可以不一样
//给nums数组分配空间,这个数组可以存储3个元素,里面的元素长度都相同,都为5
nums=new int[3][5];
//给strs数组分配空间,这个数组可以存储5个元素,里面的元素长度不相同
strs=new String[5][];3)赋值:数组名[下标][下标] = 数据;
//赋值:数组名[下标][下标] = 数据;
//给nums数组里的第一个元素(一维数组)下标为0的元素赋值为9
nums[0][0] = 9;
nums[0][1] = 8;
nums[0][2] = 18;
nums[0][3] = 28;
nums[0][4] = 38;
nums[1][0] = 48;
nums[1][1] = 58;
nums[1][2] = 68;
nums[1][3] = 78;
nums[1][4] = 88;
nums[2][0] = 98;
nums[2][1] = 81;
nums[2][2] = 82;
nums[2][3] = 83;
nums[2][4] = 84; 二维数组的声明和分配空间合并写
数据类型[][] 数组名=new 数据类型[数组长度][数组长度];
或者 数据类型[][] 数组名=new 数据类型[数组长度][];
int [][]nums=new int[5][3];
int[][]nums2=new int[3][];
String[][]strs=new String[6][5];
String[][]strs2=new String[8][]; 二维数组的声明、分配空间、赋值合并写
数据类型[][] 数组名 = new 数据类型[][]{ {数据1,数据2,...,数据n},{数据1,数据2,...,数据n},....,{数据1,数据2,...,数据n}}; (注意:new后面的两个[][]里都不能写数组长度)
int [][]nums3=new int[][]{
{1,2,3},{4,5,6},{7,8,9},{9,8,7},{6,5,4}}; 二维数组的声明、分配空间、赋值合并的简写方式
数据类型[][] 数组名 = { {数据1,数据2,...,数据n},{数据1,数据2,...,数据n},....,{数据1,数据2,...,数据n}};
int [][]nums4={
{1,2,3},{4,5,6},{7,8,9},{9,8,7},{6,5,4}};
二维数组遍历
// 二维数组遍历
int[][] nums={
{1,2,3,4},{5,6,7},{2,4,6,8,10}};
System.out.println("nums数组:");
for (int i = 0; i < nums.length; i++) {
//nums[i]又是一个一维数组,对这个一维数组nums[i]再次进行遍历,
for (int j = 0; j < nums[i].length; j++) {
System.out.print(nums[i][j]+" ");
}
System.out.println();
}
二维数组应用:
import java.util.Scanner;
public class ArrayDemo {
public static void main(String[] args) {
// 已知有3个班级各5名学员,请使用二维数组计算各个班级的总成绩
//声明一个长度为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();
}
//统计成绩
System.out.println("--------统计成绩--------");
for (int i = 0; i < scores.length; i++) {
double sum=0;//这个sum要声明在外层for循环内,外层for循环每循环一次,就声明以sum变量用来统计班级总成绩
for (int j = 0; j < scores[i].length; j++) {
sum=sum+scores[i][j];
}
System.out.println((i+1)+"班总成绩:"+sum);
}
}
}
边栏推荐
- C language explanation series - array explanation, one-dimensional array, two-dimensional array
- [chart component kit] Shanghai daoning provides developers with steema download, trial and tutorial
- There are two Kafka topics that need to write data intact to MySQL King through Flink. Scheme 1: write two f's
- DNA modified noble metal nanoparticles | DNA deoxyribonucleic acid modified metal palladium Pd nanoparticles pdnps DNA
- 解决CNN固有缺陷!通用 CNN 架构CCNN来了| ICML2022
- 2022/7/27 考试总结
- OpenTSDB-时序数据库
- Redis of non relational database [jedis client +jedis connection cluster]
- Record a MYCAT connection and solve the problems of communications link failure
- 03 | 项目部署:如何快速部署一个基于laravel框架开发的网站
猜你喜欢

DNA modified osmium OS nanoparticles osnps DNA modified iridium nanoparticles irnps DNA

单片机IO口控制12V电压通断,MOS和三极管电路

Meituan Er Mian: why does redis have sentinels?

Oracle local network service

【300+精选大厂面试题持续分享】大数据运维尖刀面试题专栏(八)
![[leetcode] 24. Exchange nodes in the linked list in pairs](/img/06/af8cffd306777b14a4989e638f5866.png)
[leetcode] 24. Exchange nodes in the linked list in pairs

Find out whether the number exists from the matrix

Parse tree structure JS

Common solutions for distributed ID - take one

深度学习基础宝典---激活函数、Batch Size、归一化
随机推荐
What happens when you unplug the power? Gaussdb (for redis) dual life keeps you prepared
Regular expression for mobile number verification
Detailed explanation of random number generated by random class
Meituan Er Mian: why does redis have sentinels?
“蔚来杯“2022牛客暑期多校训练营2补题记录(DGHJKL)
mysql,可以使用多少列创建索引?
Qt多线程中槽函数在哪个线程里执行分析
DNA modified osmium OS nanoparticles osnps DNA modified iridium nanoparticles irnps DNA
华为高级工程师---BGP路由过滤及社团属性
Lecture notes a utility for everyone to generate PCG
awk从入门到入土(16)awk变量类型探讨--关于数字和string两种类型
2022/7/27 考试总结
JS candy xiaoxiaole game source code
How to build the protection system of class protection technology of 2022 series of ISO compliance (Part I)
【活动报名】云原生技术交流 Meetup,8 月 6 日广州见
PostgreSQL is the world's most advanced open source relational database
Swm32 series tutorial 5-adc application
Common solutions for distributed ID - take one
Qt使用信号量控制线程(QSemaphore)
C language explanation series - array explanation, one-dimensional array, two-dimensional array