Java basics: 14th Array

Source: Internet
Author: User

 

An array is used to store data of the same type. in Java, an array is a type that includes two aspects: information, array objects, and elements. (1) The array definition can be defined in two formats: N type [] array name N type array name [] type is the type of elements in the array, can be the basic data type, it can be a user-defined type. There is no big difference between the two formats. The following code defines an array of date types: N date [] D1; n date D2 []; The following Code defines an integer array: N int [] I1; N int [] I2; [note] Unlike C ++, the Java language does not need to determine the number of array elements when defining arrays. (2) array Object Instantiation array initialization involves two processes: n Array Object Instantiation n Array Element instantiation array object initialization mainly determines the number of elements in the array, if the element type is not a basic data type, all element values are null. If the element type is a basic data type, the system will give the default value. The initialization format of an array object is as follows: Object Name = new type [number of elements] the following code initializes several array objects defined above: // The D1 array has five elements, d1 = new date [5]; // The D2 array has four elements, D2 = new date [4]; // The I1 array has five elements I1 = new int [5]; // The I2 array has six elements I2 = new int [6]; after instantiation, if the array element type is an object, the default value is null. The default value of basic data type elements is shown in Table 8-1. Table 8-1 default value of basic data type
Element type Default Value
Char 0 (storage value, not the character "0 ")
Byte 0
Short 0
Int 0
Long 0
Float 0.0
Double 0.0
Boolean False
Array instantiation can also be completed directly when an array is defined. The following code defines an integer array with three elements: int I2 [] = new int [3]; (3) After the access array object of the array element is instantiated, the object value is null or the default value. If it is an object, the element needs to be instantiated. To instantiate an element, You need to operate on each element. The method for accessing data elements is as follows: the index value of the array name [Index] starts from 0 to the number of array elements minus 1. If the number of array elements is five, the index value ranges from 0 to 4. If the index is greater than or equal to the number of array elements, a running error occurs: Java. Lang. arrayindexoutofboundsexceptionThe following method is used to access the elements of the D1 array defined above: d1 [0], D1 [1], d [2], d [3], d [4] (4) the new keyword is used for element initialization when the element type is basic data type ). The following code instantiate the elements of the D1 array: For (INT I = 0; I <d1.length; I ++) {D1 [I] = new date ();} each element is instantiated using new date (), but they are different objects. You can also assign values when defining arrays. The following Code assigns an array int [] I2 = {2, 3, 4, 5}; Date D2 [] = {new date (), new date (), new Date ()}; (5) After the value array of the data element is instantiated, the array element can be modified at any time. If the array element type is basic data type, you can directly access and modify it through the index. The following Code assigns one to five values to the five elements of the i1 array: For (INT I = 0; I <i1.length; I ++) {I1 [I] = I + 1;} if the array element type is an object, obtain the object first and then perform the operation. The following code modifies the information of elements in the D1 array: For (INT I = 0; I <d1.length; I ++) {D1 [I]. setdate (I + 1 );} Last Lecture: Lecture 2 Basic syntax exercises (see if you have made any mistakes) Next Lecture: 15th usageLi xucheng csdn blog: http://blog.csdn.net/javaeeteacher invites you as a friend: http://student.csdn.net/invite.php? U= 124362 & C = 7be8ba2b6f3b6cc5

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.