Declaration, initialization, and reference of 7.Java two-dimensional arrays

Source: Internet
Author: User

The declaration, initialization, and reference of a two-dimensional array is similar to a one-dimensional array, which is no longer detailed.

Definition of a two-dimensional array

Type arrayname[[];
Type [] []arrayname;
Just the difference in form, we can choose according to their own habits.

Initialization of two-dimensional arrays

1. Static initialization
int intarray[] []={{1,2},{2,3},{3,4,5}};

in the Java language, because the two-dimensional array is considered an array of arrays, the array space is not continuously allocated, so the size of each dimension of the two-dimensional array is not required.

2. Dynamic initialization
1) Allocate space directly for each dimension, in the following format:
Arrayname = new Type[arraylength1][arraylength2];
int a[] [] = new INT[2][3];

2) Allocate space for each dimension starting from the highest dimension:
Arrayname = new type[arraylength1][];
Arrayname[0] = new TYPE[ARRAYLENGTH20];
ARRAYNAME[1] = new TYPE[ARRAYLENGTH21];
...
Arrayname[arraylength1-1] = new TYPE[ARRAYLENGTH2N];

3) Example:
The dynamic initialization of an array of two-dimensional simple data types is as follows:
int a[] [] = new int[2][];
A[0] = new INT[3];
A[1] = new INT[5];
For arrays of two-dimensional composite data types, you must first allocate the reference space for the highest dimension, and then allocate the space in the lower dimension sequentially. Also, you must allocate space for each array element individually. For example:
String s[[] = new string[2][];
s[0]= new string[2];//Allocating reference space for the highest dimension
s[1]= New string[2]; Allocating reference space for the highest dimension
s[0][0]= New String ("good");//Allocate space separately for each array element
s[0][1]= new String ("Luck");//Allocate space separately for each array element
s[1][0]= New String ("to");//Allocate space separately for each array element
s[1][1]= New String ("You");//Allocate space separately for each array element

A reference to a two-dimensional array element

For each element in a two-dimensional array, it is referenced as follows:
ARRAYNAME[INDEX1][INDEX2]
For example:
NUM[1][0];

Example of a two-dimensional array: two matrix multiplication
public class matrixmultiply{
public static void Main (String args[]) {
int i,j,k;
int a[][]=new int [2][3]; Dynamic initialization of a two-dimensional array
int b[][]={{1,5,2,8},{5,9,10,-3},{2,7,-5,-18}};//Static initialization of a two-dimensional array
int c[][]=new int[2][4]; Dynamic initialization of a two-dimensional array
for (i=0;i<2;i++)
for (j=0; j<3; j + +)
a[i][j]= (i+1) * (j+2);
for (i=0;i<2;i++) {
for (j=0;j<4;j++) {
c[i][j]=0;
for (k=0;k<3;k++)
C[I][J]+=A[I][K]*B[K][J];
}
}
System.out.println ("*******matrix c********");//print Matrix C tag
for (i=0;i<2;i++) {
for (j=0;j<4;j++)
System.out.println (c[i][j]+ "");
System.out.println ();
}
}
}

Declaration, initialization, and reference of 7.Java two-dimensional arrays

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.