Basic Java language: Basic array type

Source: Internet
Author: User


1. Declaration and initialization of one-dimensional arrays, divided into dynamic and static:

View plaincopy to clipboardprint?
// Create 10 integers dynamically. The default value is 0.
Int [] a1 = new int [10];

// Create four integers statically and initialize them as values in parentheses
Int [] a2 = {1, 2, 3, 4 };

// Dynamically create a MyClass array. You need to traverse each class instance in the created array.
MyClass [] a3 = new MyClass [3];
For (int I = 0; I <a3.length; ++ I)
MyClass [I] = new MyClass ();

// Create a static MyClass array and use the instance in brackets to initialize the Array
MyClass [] a4 = {new MyClass (), new MyClass (), new MyClass };
// Create 10 integers dynamically. The default value is 0.
Int [] a1 = new int [10];
 
// Create four integers statically and initialize them as values in parentheses
Int [] a2 = {1, 2, 3, 4 };
 
// Dynamically create a MyClass array. You need to traverse each class instance in the created array.
MyClass [] a3 = new MyClass [3];
For (int I = 0; I <a3.length; ++ I)
MyClass [I] = new MyClass ();
 
// Create a static MyClass array and use the instance in brackets to initialize the Array
MyClass [] a4 = {new MyClass (), new MyClass (), new MyClass };
 

2. Multi-dimensional arrays can be understood as arrays. They can also be declared in two forms:

View plaincopy to clipboardprint?
Public class Main {
Static void printInt (int [] [] ints ){
For (int I = 0; I <ints. length; ++ I)
For (int j = 0; j <ints [I]. length; ++ j)
System. out. println (ints [I] [j]);
}

Public static void main (String [] args ){
// Dynamically create a two-dimensional array and traverse the initialization
Int [] [] ints = new int [2] [2];
For (int I = 0; I <ints. length; ++ I)
For (int j = 0; j <ints [I]. length; ++ j)
Ints [I] [j] = I + j;
PrintInt (ints );

// Create a two-dimensional array statically
Int [] [] ints2 = {1, 2}, {3, 4 }};
PrintInt (ints2 );
}
}
Public class Main {
Static void printInt (int [] [] ints ){
For (int I = 0; I <ints. length; ++ I)
For (int j = 0; j <ints [I]. length; ++ j)
System. out. println (ints [I] [j]);
}
 
Public static void main (String [] args ){
// Dynamically create a two-dimensional array and traverse the initialization
Int [] [] ints = new int [2] [2];
For (int I = 0; I <ints. length; ++ I)
For (int j = 0; j <ints [I]. length; ++ j)
Ints [I] [j] = I + j;
PrintInt (ints );
 
// Create a two-dimensional array statically
Int [] [] ints2 = {1, 2}, {3, 4 }};
PrintInt (ints2 );
}
}

3. variable array declaration form:

View plaincopy to clipboardprint?
Public class Main {
Static void printInt (int [] [] ints ){
For (int I = 0; I <ints. length; ++ I)
{
For (int j = 0; j <ints [I]. length; ++ j)
System. out. print (ints [I] [j] + "");
System. out. println ();
}
}

Public static void main (String [] args ){
// Dynamically create a two-dimensional variable array, first create the first dimension
Int [] [] ints = new int [2] [];
// Determine the number of elements in the second dimension.
For (int I = 0; I <ints. length; ++ I)
Ints [I] = new int [I + 3];
PrintInt (ints );

// Create a two-dimensional variable array statically
Int [] [] ints2 = {1, 2}, {3, 4, 5 }};
PrintInt (ints2 );
}
}
// The output is:
0 0 0
0 0 0 0
1 2
3 4 5

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.