当前位置:网站首页>二维数组及操作
二维数组及操作
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);
}
}
}
边栏推荐
- PMP practice once a day | don't get lost in the exam -7.13
- How to build the protection system of class protection technology of 2022 series of ISO compliance (Part I)
- What are the different tables in MySQL?
- Plantuml Usage Summary
- 演讲笔记 适合所有人的实用程序生成 PCG
- What if the task manager is not fully displayed?
- No super high-rise buildings | new regulations: what information does it reveal that no new buildings above 500 meters should be built?
- There are two Kafka topics that need to write data intact to MySQL King through Flink. Scheme 1: write two f's
- Recommend a fully open source, feature rich, beautiful interface mall system
- [Err] 1055 - Expression#2 of select list is not in GROUP BY clause and contains nonaggregated column
猜你喜欢

GD32使用ST的HAL库和GD官方库的一些体会

Can the variable modified by final be modified

Deep browser rendering principles

Es6: arrow function usage

Allure use

Qt使用信号量控制线程(QSemaphore)

EMC design strategy - clock

解决EMC、EMI传导干扰的八大方法

No super high-rise buildings | new regulations: what information does it reveal that no new buildings above 500 meters should be built?

Some experience of gd32 using Hal Library of ST and Gd official library
随机推荐
CarSim simulation quick start (10) - Modeling of braking system
一键开关机电路
CarSim simulation quick start (XI) - Driver Model (1)
Es6: arrow function usage
Plantuml Usage Summary
The core packages and middleware required for golang development cover all areas of the project and are worth collecting
JS cartoon English alphabet typing game source code
Lecture notes a utility for everyone to generate PCG
JS thoroughly understand this point
[300 + selected interview questions from big companies continued to share] big data operation and maintenance sharp knife interview question column (VIII)
Regular expression for mobile number verification
Elaborate on common mode interference and differential mode interference
Openstack dashboard configuring public network access
Basic dictionary of deep learning --- activation function, batch size, normalization
Common solutions for distributed ID - take one
非关系型数据库之Redis【redis集群详细搭建】
js卡通英文字母打字小游戏源码
SWM32系列教程5-ADC应用
How to understand the adjective prefix of socket: "connection oriented" and "connectionless"
五张图看懂EMI电磁干扰的传播过程-方波陡峭程度对高频成分的影响,时序到频域频谱图形,波形形状对EMI辐射的影响。