Java Handout Experience (iii) array Understanding __java Basics

Source: Internet
Author: User

(1) must understand before using the array is also a type, Java array requires all array elements are the same data type, so in an array, the element type in the array is unique, that is, an array can only store one data type, rather than storing a variety of data types

One-dimensional array initialization has 2 different formats

1. Static initialization, when initialized by the programmer to specify the initial value of each array element, the system determines the length

2. Dynamic initialization, when initialized by the programmer to specify the length, by the system array elements to allocate the initial value

Static initialization definition

Arrar[] Arrayname={element1,...}

Dynamic initialization definition

Arrayname=new Type[length], when performing dynamic initialization, the programmer only needs to specify the length, that is, to specify the desired memory space for each array element, the system will assign an initial value to these array elements, and when the initial value is specified, the system assigns the initial value according to the following rules

@ Integer data type (Byte,int,short and long) the value of the element is 0

@ floating-point type (float,double), the value of the element in the array is 0.0

@ character type (char), the value of the element in the array is ' \u0000 '

@ Boolean type (Boolen), the value of the element in the array is false

@ Reference types (classes, interfaces, and arrays), the elements in the array are null

Note that you do not use both static initialization and dynamic initialization, that is, do not specify the length of the array, or assign an initial value to each array element

(2) Deep array

After the array is defined and initialized, 2 spaces are allocated in memory, one to hold the array itself, and one user to hold the array reference variable

Two-dimensional array

Two-dimensional array definition type[][] arrayname

In Java, the above is used to define a two-dimensional array, in fact, the essence is a one-dimensional array, but its array elements are also references, the array element holds the reference to the one-dimensional array, and then to the ' two-dimensional array ' to perform initialization, the same can be used as a one-dimensional array initialization, the ' Two-dimensional array ' as a one-dimensional array, whose element type is type[] type, can be initialized with Arrayname=new type[lengtj][].

public class test{

public static void Main (String args[]) {

Initialize a as a one-dimensional array, initializing A is an array of length 4

Int[] [] a=new int[4][];

Loop A to a one-dimensional array

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

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

Initializes the first element in array a

A[0]=new int[2];

Access the first element in the a array to point to the 2nd element of the index group

a[0][2]=6;

for (int i=0;i<a[i].length;i++) {

System.out.println (A[0][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.