当前位置:网站首页>Two dimensional array summation exercise
Two dimensional array summation exercise
2022-07-27 05:08:00 【if you never leave me, i will be with you till death do us apart】
Two dimensional array : There are three ways to initialize arrays : initiate static 、 dynamic initialization 、 Default initialization
Two dimensional array :[][]
initiate static
In addition to using new Keyword to generate array outside , You can also allocate space and assign values to array elements while defining arrays .
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}};
dynamic initialization
The definition of an array is separated from the operation of allocating space and assigning values to array elements .
eg:
int[][] arr = new int[3][]; // In essence, the length of one-dimensional array is defined as 3, Every “ lattice ” What is put in is an array
arr[0] = {
1,2};
arr[1] = {
3,4,5,6};
arr[2] = {
34,45,56};
describe
Given a two-dimensional array , Please write a summation function , Calculate the sum of the array elements
Output description :
Outputs the sum of two-dimensional array elements
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}};
// Method body 1 Two dimensional array
int sum=add(arr);
System.out.println(sum);
// Method body 2 Singular group
int[] arr1 = {
5,10,5,10,5,5};
int sum1=add1(arr1);
System.out.println(sum1);
}
// utilize for Loop iteration Additive operation
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;
}
Running results
5175
40
边栏推荐
- Final Cut Pro中文教程 (2) 素材窗口的认识
- 自定义视口高度,多余的部分使用滚动
- Replication of df-gan experiment -- detailed steps of replication of dfgan and forwarding from remote port to local port using mobaxtem
- Counting Nodes in a Binary Search Tree
- Solution: how to use bash batch command in win10
- Knapsack problem DP
- MySQL storage engine and its differences
- 测试基础5
- 使用ngrok做内网穿透
- 34. 分析flexible.js
猜你喜欢

Sunset red warm tone tinting filter LUTS preset sunset LUTS 1

Jmeter 界面如何汉化?

Introduction to Web Framework

会议OA之我的审批

TypeScript 详解

QT menu bar, toolbar and status bar

Introduction to dynamic memory functions (malloc free calloc realloc)

Installation and template setting of integrated development environment pychar

Introduction to MySQL optimization

"Photoshop2021 tutorial" adjust the picture to different aspect ratio
随机推荐
来自“飞人”乔丹的启示!奥尼尔开启的另一个“赛场”
Could not autowire.No beans of ‘userMapper‘ type found.
Acceptance and neglect of events
Install pyGame
1、 MySQL Foundation
Use of file i/o in C
Unit test Chapter6
Svn usage details
安装Pygame
[Niuke discussion area] Chapter 7: building safe and efficient enterprise services
Sub database and sub table
Customize the viewport height, and use scrolling for extra parts
【牛客讨论区】第七章:构建安全高效的企业服务
Web框架介绍
Flexible array and common problems
老子云携手福昕鲲鹏,首次实现3D OFD三维版式文档的重大突破
事件(event)
TypeScript 详解
R-score reproduction R-Precision evaluation index quantitative text generation image r-score quantitative experiment whole process reproduction (R-Precision) quantitative evaluation experiment step on
Solution: how to use bash batch command in win10