Javase Basics (4)-Application of arrays (4.3 Two-dimensional arrays)

Source: Internet
Author: User

First, understand

The two-dimensional array itself is also a reference type , and the saved address number
A two-dimensional array holds a container for multiple one-dimensional arrays, which is equivalent to the combination of multiple one-dimensional arrays, that is, each element in a two-dimensional array is a one-dimensional array

Second, the use of the array steps1. Dynamic initialization

(1) declares the
data type [] array name;
  or
data type [] array name [];
or
Data type array name [];
(2) Open Space
format one: fixed number of columns
Array name = new data type [number of rows] [number of columns];//number of rows corresponds to the length of a two-d array or the number of one-dimensional arrays, Number of columns equal to the number of members in each one-dimensional array
format two: Not fixed number of columns
Array name = new data type [ number of rows ] [];
The first two steps can be merged
Syntax example:
int[][] arr = new int[5][];
(3) Assignment
Format one: fixed number of columns
for (int i=0;i< array name. length;i++) {
for (int j=0;j< array name [i].length;j++) {
Array] name [I][j] = value;
}
}
Format two: Number of non-fixed columns
for (int i=0;i< array name. length;i++) {
array name [i]=new data type [length];//length cannot be omitted!       
for (int j=0;j< array name [i].length;j++) {
array name [I][J] = value;
}
}
(4) Use
for (int i=0;i< array name. length;i++) {
for (int j=0;j< array name [i].length;j++) {
Sy STEM.OUT.PRINTLN (array name [I][J]);
}
}

2. Static initialization

(1) Declaring and initializing
data type [] array name ={{value, value},{value, value, value}};
data type [] array name =new data type [][]{{value, value},{value, value, value}};
(2) Use
for (int i=0;i< array name. length;i++) {
for (int j=0;j< array name [i].length;j++) {
SYSTEM.OUT.PRINTLN (array name [I][J]);
}
}

Javase Basics (4)-Application of arrays (4.3 Two-dimensional arrays)

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.