当前位置:网站首页>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
边栏推荐
猜你喜欢

Raspberry pie output PWM wave to drive the steering gear

Inspiration from "flying man" Jordan! Another "arena" opened by O'Neill

文件对话框

使用mq消息队列来进行下单流程的高并发设计,消息挤压,消息丢失,消息重复的产生场景和解决方案

【搜索】—— 多源BFS + 最小步数模型

Web框架介绍

Installation and template setting of integrated development environment pychar

智慧展厅设计的优势及适用行业分析

File dialog box

Final Cut Pro Chinese tutorial (2) understanding of material window
随机推荐
How does the TCP server handle multiple client connections on one port (one-to-one or one to many)
精选用户故事|洞态在聚水潭的误报率几乎为0,如何做到?
MySQL storage engine and its differences
会议OA之我的审批
How to sinicize the JMeter interface?
STM32_ HAL_ SUMMARY_ NOTE
ssm框架整合
Flexible array and common problems
"Photoshop2021 tutorial" adjust the picture to different aspect ratio
自定义视口高度,多余的部分使用滚动
Sub database and sub table
MySQL download and installation & perfect uninstall
使用mq消息队列来进行下单流程的高并发设计,消息挤压,消息丢失,消息重复的产生场景和解决方案
QT menu bar, toolbar and status bar
Tcp server是如何一个端口处理多个客户端连接的(一对一还是一对多)
智慧展厅设计的优势及适用行业分析
二维数组求和 练习
Introduction to Web Framework
集合框架的使用
There is no need to install CUDA and cudnn manually. You can install tensorflow GPU through a one-line program. Take tensorflow gpu2.0.0, cuda10.0, cudnn7.6.5 as examples