Java programming 44-Basic array syntax

Source: Internet
Author: User

Java programming those things 44-array of basic syntax Zhengzhou game college Chen yuefeng from: http://blog.csdn.net/mailbomb 6.2 basic array syntaxAfter learning about the concept of arrays, let's take a look at the syntax format of arrays. There are four syntax formats for Arrays: array declaration, array initialization, referencing array elements, and obtaining array length. 6.2.1 array declarationSimilar to variables, arrays must be declared before they are used. The syntax format of arrays is: data type array name [] or: Data Type [] array name, for example: int M []; char C []; double d []; the data type here can be any data type of the Java language, that is, it can be either a basic data type or a composite data type. Use a pair of brackets when declaring an array. The brackets can be placed either after the data type or after the array name. The array name is an identifier. You can set its name as needed. Use this name in a program to represent the array. The syntax formats of these two statements are fully equivalent in actual use and can be used according to habits. The array declaration does not occupy space in the memory and has no address. Because the array is a composite data type, the default value is null after the declaration is complete. Arrays cannot be used directly after they are declared. They must be initialized before they can be used. 6.2.2 array InitializationArray Initialization is to assign values to the array. The array initialization syntax is divided into two types: static initialization and dynamic initialization. Static initialization applies to the values of all elements in the known array. All elements are initialized at a time, and only space is applied for dynamic initialization. The value of each element is the initial value corresponding to the Data Type During array declaration. 6.2.2.1 static InitializationStatic initialization, also known as the overall value assignment of an array, is a syntax that assigns values to all elements in the array in sequence. You can use the syntax to specify the values of each element in the array, the length of the array is also specified. Syntax format: Data Type [] array name = {value 1, value 2 ,......, Value: n}; for example: int [] M = {1, 2, 3, 4}; char C [] = {'A', 'F', 'D '}; static initialization must be in the same row as the declaration of the array. In other words, static initialization can only be performed when the array is declared. All elements in the array are written inside a pair of braces, and the system assigns values to the array operation in sequence according to the writing order of values. For example, if the array m is used, 1 is assigned to the first element of the M array, 2. Assign the value to the second element of the M array, and so on. The total length of the array is equal to the number of values during static initialization. In actual writing, you must note that the value type must match the type when the array is declared, or it can be automatically converted. In the actual program, static initialization generally writes a set of known irregular values, which is relatively simple to write and the format is relatively unified. 6.2.2.2 dynamic InitializationDynamic initialization, that is, only specifying the length of the array and applying for space in the memory. Dynamic initialization does not have to be put together with the array declaration, or you can reinitialize an initialized array. Syntax format for dynamic initialization: Data Type [] array name = new data type [length]; for example: int [] M = new int [10]; char [] C; N = new char [3]; Dynamic initialization uses the New Keyword for initialization. The subsequent data types of the New Keyword are the same as those of the array declaration, inside the brackets is the length of the array to be initialized. The length value can be a number or an integer variable. If it is an integer variable, it cannot be a long variable. In actual use, you can declare it before dynamic initialization. Dynamic initialization specifies the length of the array and applies for the corresponding length space in the memory. The value of each element is the default value corresponding to the array data type. The default values are as follows: the default values of the and Boolean types are false. B. the other seven basic data types are 0. Note: The default value of char is 0 rather than 0. C. The initial value of the composite data type is null. Dynamic initialization only applies for the corresponding length of space for the array. The values of the stored elements can be specified as needed. 6.2.3 reference array elementsAn array is a set of numbers. In actual use, each element in the array needs to be referenced. The syntax format of the element referenced in the array is: array name [subscript], where the index value of each element in the subscript index group. Java syntax specifies that the index value of the first element in the array is 0, the second is 1, and so on. During program writing, both constants and variables can be written at the subscript position. The expression of the referenced element can be regarded as a variable. The type of the variable is the same as that of the array. The sample code is as follows: int [] M = {3, 2, 4, 6}; m [1] = 4; m [2] = m [3] + M [0]; the variable can be used as the subscript in the Code. The sample code is as follows: Char [] CH = new char [10]; int I = 2; ch [I] = 'a '; using variables as the subscript of an array greatly enhances the flexibility of array elements and must be deeply understood when using arrays flexibly. Because the subscript of an array starts from 0, the range of valid array subscript ranges from 0 to the length of the array minus 1. Other subscript values are invalid. If an invalid subscript appears in the code, no syntax error occurs, but an exception occurs during running. 6.2.4 obtain the array LengthTo facilitate Array Operations, Java Syntax provides the syntax format for obtaining the array length. For an array that has been initialized, the syntax format for obtaining the array length is: array name. the length sample code is as follows: int [] n = {1, 2, 3, 4, 6}; int Len = n. length; N. length indicates the length of array n. the array initialization shows that the length of array n is 5, and the value of the variable Len is 5. With this syntax, you only need to know the name of the array to obtain the length of the array, so that you can flexibly operate the array. Based on the syntax and length of the previous lower mark, the code of all elements in the output array n is: For (INT I = 0; I <Len; I ++) {system. out. println (N [I]);} in this way, arrays are called traversal. traversing arrays is the basis of arrays and also the basis for many logic implementations related to arrays. The array syntax is described as follows. The following examples are used to demonstrate the actual use of 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.