Java Basic Note (4)----array

Source: Internet
Author: User

    1. Introduced:

      An array is a data type, a reference type, and a contiguous memory space for storing and managing multiple data of the same type.

    2. Definition:--> How arrays are declared
      1. First declare, in open memory space--int [] A; A=new Int[5];
      2. Simultaneous static initialization of declarations--> int[] a={1,3,5}
      3. Declaring and opening up memory space->int [] a=new int[5]
    3. Use:--> array name [subscript]: The valid range of subscript is: 0 to length-1;
      1. Existing elements:
      2. Take element:
      3. To iterate over an array:

      The code is as follows:

Package com.lvsling.test;

Public class Test1 {

Public staticvoid main (string[] args) {

        // define an array

int[] a=newint[5];

        // Save Data

A[0]=1;

a[1]=3;

a[2]=5;

a[3]=7;

a[4]=9;

        // Fetch Data

System. out. println (a[2]);

System. out. println (A[4]);

        // iterating through the elements in an array

For (int i = 0; i < A.length; i++) {

System. out. println (A[i]);

}

}

}

    1. Array expansion
      1. Creates a new array,
      2. Copy the contents of the original array into the new array
      3. Returns the new array

      The code is as follows:

Package com.lvsling.test;

/*

* Expansion

*/

Public class Test2 {

Public staticvoid main (string[] args) {

int[] b={1,2,3,4,5};

b=Expand3(b);

For (int i=0;i<b.length; i++) {

System. out. println (B[i]);

}

}

        // the loop assigns all the elements in the original array to the new array, individually.

Public staticint[] expand1 (int[] a) {

int[] b=newint[9];

For (int i=0;i<a.length; i++) {

B[i]=a[i];

}

return b;

}

         //system.arraycopy ( meta array new array new array start length

Public staticint[]expand2 (int[] a) {

int[] b=newint[7];

System. arraycopy (a,1,b,2,3);

return b;

}

//java.util.arrays.copyof ( original array , new length );

Public staticint[]expand3 (int[] a) {

return java.util.Arrays. CopyOf(a,10);

}

}

    1. Self-Summary:

      The above describes the declaration of arrays, use, traversal, and the expansion of arrays. The core is to open up memory space to store the data we need.

      Expansion and Promotion:

      1. The array element has a default value.
        1. After the array has opened up memory space, the value is assigned by default, without assigning values to the elements.
          1. Integer--------->0
          2. Decimal--------->0.0
          3. Boolean--------->false
          4. Character---------> ' \u0000 '
          5. Reference type--------->null

      2, two-dimensional array:

      1, is an array of one-dimensional arrays

      2, when opening up the memory space, at least to specify the number of dimensions of the row;

      3, the static initialization of the declaration must be done in the same line of code;

      4, open memory + static data to open up memory rows and columns of the dimension can not be specified.

Java Basic Note (4)----array

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.