Java two-dimensional array

Source: Internet
Author: User

How to define a two-dimensional array

Data type "" "Name = new data Type" a "" B "

A: There are several one-dimensional arrays represented in this two-dimensional array

B: How many elements are represented in this one-dimensional array, indexed

Data type "" "" name = {{},{},{}};

Traversing a two-dimensional array

Class Lianxi {public static void main (string[] args) {int[][] attr ={{1,2},{3,4},{5,6}};for (int i =0;i<attr.length; i+ +) {for (int j=0;j<attr[i].length; j + +) {System.out.println (attr[i][j]);}}}

Examples:

For example, you want to count the total sales of each group in a company's three sales groups and the sales for the entire company. As shown below

The first group of sales is {11, 12} million

The second group of sales is {21, 22, 23} million

The third group of sales is {31, 32, 33, 34} million.

classLianxi { Public Static voidMain (string[] args) {intA = 0; Define 2 countersintb = 0; int[] attr ={{11,12},{21,22,23},{31,32,33,34}};  for(intI =0;i<attr.length;i++)//Outer loop control each group { for(intJ=0;j<attr[i].length; j + +)//Inner loop controls the addition of elements in a one-dimensional array to get the sales of each group {a= attr[i][j]+A; } System.out.println ("+i+" group sales Total "+"a); Sales for each group B= A +b; Assign a value to B, then clear A to zero, B is the annual sales a= 0; }system.out.println ("Company Annual Sales" +b); }}

  

Java two-dimensional array

Related Article

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.