Knowledge about arrays in Java

Source: Internet
Author: User

1.

2. Naming Methods for arrays

1) int[]ages=new int[5];

2) Int[]ages;

Ages=new Int[5];

3, Java does not support different types of duplicate names array

4. Cyclic assignment of arrays in Java

1    PackageDierge;2   3    Public classShuzu {4      5       Public Static voidMain (String args[]) {6          int[]ags=New int[5];7          inti;8           for(i=0;i<ags.length;i++){9ags[i]=i;TenSystem.out.println ("ags[" +i+ "] is:" +ags[i]); One         } A  -  -  the     } -  -}

The printing results are as follows:

Ags[0] is: 0
AGS[1] is: 1
AGS[2] is: 2
AGS[3] is: 3
AGS[4] is: 4

5. Initial values for various types of arrays

The code is as follows:

1  PackageDierge;2 3  Public classShuzu {4     5      Public Static voidMain (String args[]) {6         int[]ags=New int[1];7SYSTEM.OUT.PRINTLN ("The default value for an array of type int is:" +ags[0]);8         Boolean[]a=New Boolean[1];9SYSTEM.OUT.PRINTLN ("The default value of the Boolean array is:" +a[0]);Ten         byte[]b=New byte[1]; OneSYSTEM.OUT.PRINTLN ("The default value for an array of type Byte is:" +b[0]); A          Short[]c=New  Short[1]; -SYSTEM.OUT.PRINTLN (the default value for an array of type short is: "+c[0]); -         Char[]d=New Char[1]; theSYSTEM.OUT.PRINTLN (the default value for an array of type char is: "+d[0]); -         Long[]e=New Long[1]; -SYSTEM.OUT.PRINTLN ("The default value for an array of type Long is:" +e[0]); -         float[]f=New float[1]; +SYSTEM.OUT.PRINTLN (the default value of the "float type array" is: "+f[0]); -         Double[]g=New Double[1]; +SYSTEM.OUT.PRINTLN ("The default value for an array of type Double is:" +g[0]); A          at          -          -  -          -          -     } in  -}

The printing results are as follows:

The default value for an array of type int is: 0
The default value for an array of type Boolean is: false
The default value for an array of type Byte is: 0
The default value for array of type short is: 0
The default value for an array of type char is:

The default value for an array of type Long is: 0
The default value for an array of type float is: 0.0
The default value for an array of type Double is: 0.0

Knowledge about arrays in Java

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.