One-dimensional array declaration, creation, initialization

Source: Internet
Author: User

first, the definition of the array

Definition of an array: An ordered collection of the same data type . Each of these data is called an array of elements, and each element can be accessed by subscript to "start with subscript 0". Arrays are also objects

Need to note:

1, the length of the array is determined, once the array is created, its size cannot be changed

2. The elements in the array can be any data type, including the base type and the reference type. "But the data type must be the same"

3. The array itself is the object, and each element in the array is equal to the member variable of the object

4. Array objects exist in the heap

5. Only when instantiating an array object does the JVM allocate space, which is related to length.

6. You cannot specify the number of array elements when declaring. For example: int a [5]; Error

7, the construction of an array must specify the length.

Ii. declaration, creation, initialization of arrays
1234567891011121314151617181920212223242526272829303132333435 //1、数组的声明int[] a;int[] b;//2、数组的创建:直接newa = new int[3];//此处new int[3]必须指定长度,假如new int[]这样写会编译不过去//3、数组的初始化:3种方式//3.1、默认初始化:数组是引用类型,它的元素相当于类的成员变量,因此数组分配空间后,每个元素也被按照成员变量的规则被隐士初始化//数组的元素相当于对象的成员变量,默认值跟成员变量的初始化规则一样。比如数字:0,布尔:false,char:\u0000 ,引用类型:nullint[] arr = new int[4];//1、直接这样new的话数组已经默认初始化了,//3.2、静态初始化:在定义数字的同时就为数组元素分配空间并赋值int[] arr2 = new int[] {1,2,3,4};int[] arr3 =  {1,2,3,4};//3.3、动态初始化:数组定义与为数组分配空间和赋值的操作分开进行//静态初始化 完整写法int[] arr4 = new int[] {1,2,3,4,5,6};int[] arr5 =null;arr4 =new int[]{1,2,3};//简写 推荐 必须 声明与赋值同时进行int[] arr6 = {1,2,34,6};int[] arr7 =null;//arr7 ={1,2,3,4}; //错误:简写不能与声明分开//int[] arr8 = new int[5] {1,2,3,4,5,6};//错误:指定了长度int[] arr9 =null;arr9 = new int[6];arr[9] =1;//循环动态初始化int[] arr10 = new int[3];for (int i=0;i<arr10.length; i++ ) {    arr10[i] = i;}

One-dimensional array declaration, creation, initialization

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.