A brief introduction to Java Array (i)

Source: Internet
Author: User

Array:

The most common type of data structure in a programming language is an array of contiguous storage spaces in memory. can be used to store multiple data, each array element holds one data, and the array element is usually accessed through the index of the array element.

1.java Arrays:

 With the same data type, the length is fixed, and the data in the array is accessed by index or subscript. (An array is also a data type, which is itself a reference type)

2. define the array:

syntax: type [] arrayname;     or type arrayname[]; It is recommended to use the first format, which has better readability.

Note: Defining an array is not the length of the specified array.

3. Initialization of the array:

The so-called initialization is to allocate the memory space for array elements of arrays and assign an initial value to each array element. The initial value is obtained in two forms: 1, automatically assigned by the system, and 2 by the programmer.

Static initialization: When initialized, the programmer displays the initial value of the specified array element, and the system determines the array length.

Grammar:

Arrayname=new Type[]{e1,e2,e3,.....}; Where type is the data type of the array element.

Dynamic initialization: The programmer only specifies the length of the array when initializing, and the system assigns the initial value to the set of elements.

Grammar:

Arrayname=new Type[length]; Where type is the data type of the array element.

 //  notation 1   int  []array=new  int  []{12 , 14 , 57 , 22  };  //  2: Simplified notation  int  []array={12 , 14 , 57 , 22  };  //  notation 3: Specify only the length of the array  int  array[]=new  int  [4 ]; 

Note: Do not use static initialization and dynamic initialization at the same time, that is, do not specify both the length of the array and the initial value for each array element when initializing the array.

Note: The system allocates an array initialization value rule:

1 The integer type in the base type (byte, int, short, long) The array element value is 0;

2 The floating-point type (float, double) in the base type is the value of the array element is 0.0;

3 The character type (char) in the base type is the value of the array element is "\u0000";

4 The Boolean type in the base type is the value of the array element is false;

5 array element type is a reference type (class, interface, array) the value of the array element is null;

4. Using Arrays  

1 accesses an array element (including assigning values to array elements and removing the values of the elements of the arrays), using the array's Length property.

The index of the array is starting from 0!!! The index value of the first element is 0, and the index value of the last array element is the array length minus 1 (length-1).

Note: The index is less than 0 or greater than or equal to the length of the array, the compiler will not have any errors, but the runtime will have an exception type of: ArrayIndexOutOfBoundsException (array out of bounds exception)

int []array={1,2,3,4,5,6,7,8,9 }; // The 1th element of the output System. out. println (array[0]); // assigns a value of 3rd element array[2]=9;

2 times the array element (for loop traversal, foreach traversal)

// iterate through the following array String [] array={"Andy Lau", "Aaron Kwok", "Jacky", "Dawn"};//forloop traversal for (int i=0 ; i<array.length;i++) {   System.  out . println (Array[i]);                } // foreach Traversal  for (String arr:array) {   System.  out . println (arr);       }

Note: When using a Foreach loop to iterate over an algebraic group element, you cannot change the value of the array element, so do not assign a value to the loop variable of the foreach.

A brief introduction to Java Array (i)

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.