Java array traversal and summation knowledge points, java array knowledge points

Source: Internet
Author: User

Java array traversal and summation knowledge points, java array knowledge points

One-dimensional array traversal and summation:

Public class OneArry {public static void main (String [] args) {double [] num = {1.9, 2.9, 3.4, 3.5, 10,-1 }; num [0] = new Double (2); // change 1.9 to 2.0 double sum = 0 by subscript; for (int I = 0; I <num. length; I ++) {// traverse System. out. println (num [I]); sum + = num [I]; // sum} System. out. println ("sum of one-dimensional arrays" + sum );}}

Running result:

2.02.93.43.510.0-1.0 the sum of one-dimensional arrays is 20.8.

Two-dimensional array traversal and summation: (method 1)

Public class TwoArry {public static void main (String [] args) {int sum = 0; int num [] [] = {1, 2, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}, {16, 17, 18, 19, 20}, {, 25}; System. out. println ("output two-dimensional array:"); num [0] [0] = new Integer (100 ); // Change 1 in the array to 100 for (int I = 0; I <num. length; I ++) {// traverse, arrange for (int j = 0; j <num [I]. length; j ++) {System. out. print (num [I] [j] + "\ t"); sum + = num [I] [j]; // sum} System. out. println ();} System. out. println ("sum of two-dimensional arrays:" + sum );}}

Running result:

Output two-dimensional array: 1002345678910111213141516171819202122232425 sum of two-dimensional arrays: 424

Two-dimensional array traversal and summation: (method 2)

Public class TwoArry {public static void main (String args []) {int [] [] num = new int [5] [5]; int count = 1; // The array starts from 1 and the int sum = 0; for (int I = 0; I <num. length; I ++) {for (int j = 0; j <num [I]. length; j ++) {num [I] [j] = count ++; // Add count to the array sum + = num [I] [j]; // sum // System. out. println (num [I] [j]); // print all arrays} System. out. println ("sum of two-dimensional arrays:" + sum); System. out. println ("output two-dimensional array:"); for (int I = 0; I <num. length; I ++) {// arrange for (int j = 0; j <num [I]. length; j ++) {System. out. print (num [I] [j] + "\ t");} System. out. println ();}}}

Running result:

Sum of two-dimensional arrays: 325 outputs two-dimensional arrays: 12345678910111213141516171819202122232425

Note: num. length indicates the number of rows, and num. length [I] indicates the number of columns.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.