当前位置:网站首页>二维数组求和 练习
二维数组求和 练习
2022-07-27 04:39:00 【你若不离不弃,我必生死相依】
二维数组:数组的初始化方式总共有三种:静态初始化、动态初始化、默认初始化
二维数组:[][]
静态初始化
除了用new关键字来产生数组以外,还可以直接在定义数组的同时就为数组元素分配空间并赋值。
eg:
int[][] arr = {
{
1,2},{
4,5,6},{
4,5,6,7,8,9,9}};
int[][] arr =new int[][] {
{
1,2},{
4,5,6},{
4,5,6,7,8,9,9}};
动态初始化
数组定义与为数组元素分配空间并赋值的操作分开进行。
eg:
int[][] arr = new int[3][]; //本质上定义了一维数组长度为3,每个“格子”中放入的是一个数组
arr[0] = {
1,2};
arr[1] = {
3,4,5,6};
arr[2] = {
34,45,56};
描述
给定一个二维数组,请你编写一个求和函数,计算出这个数组元素的和
输出描述:
输出二维数组元素的和
public static void main(String[] args) {
int[][] arr = {
{
11,33,50},{
22,44,66,88},{
131,214,315,146},{
928,827,726,625},{
424,525}};
//方法体1 二维数组
int sum=add(arr);
System.out.println(sum);
//方法体2 单数组
int[] arr1 = {
5,10,5,10,5,5};
int sum1=add1(arr1);
System.out.println(sum1);
}
//利用for 循环迭代进行 相加的操作
private static int add1(int[] arr1) {
int sum=0;
for(int i=0;i<arr1.length;i++){
sum=sum+arr1[i];
}
return sum;
}
public static int add(int[][] arr) {
int sum=0;
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr[i].length;j++){
sum=sum+arr[i][j];
}
}
return sum;
}
运行结果
5175
40
边栏推荐
- Text processing tool in shell, cut [option parameter] filename Description: the default separator is the built-in variable of tab, awk [option parameter] '/pattern1/{action1}filename and awk
- Structural mode - facade mode
- Shift right of negative numbers
- Shell programming enhancements
- HCIA static routing comprehensive experiment
- Interesting C language
- 如何做数据平滑迁移:双写方案
- TCP three handshakes and four disconnects
- 【搜索】Flood Fill 和 最短路模型
- Session&Cookie&token
猜你喜欢

小程序项目如何创建
![[day02] Introduction to data type conversion, operators and methods](/img/81/e2c49a4206e5d0d05308a1fc881626.png)
[day02] Introduction to data type conversion, operators and methods
Full revolutionary networks for semantic segmentation (FCN)

CDH集群集成外部Flink(改进版-与时俱进)

利用Power Automate,轻松下载Power BI报告中的数据

网络协议详解:IP

Yolov4 network details

Safety fourth after class exercise

Chapter 4 scope and life cycle of bean object

.htaccess learning
随机推荐
Reproduce ssa-gan using the nine day deep learning platform
Stm32 cubemx hal----- PWM - change frequency
Final Cut Pro中文教程 (2) 素材窗口的认识
STM32 Hal serial port (uart/usart) debugging experience (I) -- basic knowledge of serial port communication +hal library code understanding
Title: there is an array that has been sorted in ascending order. Now enter a number and ask to insert it into the array according to the original rule.
Text processing tool in shell, cut [option parameter] filename Description: the default separator is the built-in variable of tab, awk [option parameter] '/pattern1/{action1}filename and awk
消防安全培训资料汇总
Qstring conversion char*
【C语言】动态内存管理
Full revolutionary networks for semantic segmentation (FCN)
小程序项目如何创建
Chapter 6: cloud database
Yolov4网络详解
Two way republication experiment
5.component动态组件的展示
背包问题dp
Cloudcompare & PCL match point distance suppression
Digital integrated circuit: MOS tube device chapter (II)
Vscode opens a new chapter in the visualization of pull request update code branches
【报错】Cannot read property ‘parseComponent‘ of undefined