Declaration and use of java-arrays

Source: Internet
Author: User

1. Overview of Arrays (reference types)

    • An array is a data structure that stores the same type
    • Array variable belongs to reference data type
    • The elements in the array can be any data type (base type and reference type)
    • Once an array is created, its size cannot be changed , but the elements in the array can be changed .

2. Initialization of arrays

Once an array is created successfully, it will complete the following three actions

    1. Create an Array object
    2. Allocating storage space to an array in memory
    3. Initialize default values for elements of an array

3. Creation of an array of basic data types (when no value is assigned)

Array of type int: default value is 0

Array of type Boolean: false by default

4. Creation of an array of reference data types (when no value has been assigned)

The default value is NULL, because the array is also a reference type, and the element is also a reference type, so the array points to the array element, and the array element points to its value

5. Enhanced for Loop

Added an enhanced for loop syntax in JDK5.0

for (type Element:array)

{System.out.println (element);}

int arr[]={1,32,345,2};

for (int element:arr)

{System.out.println (element+ "");}

Pros: Ideal for traversing the contents of an element in an array or collection

Disadvantage: Unable to access the subscript of an array

6. Two-dimensional array (array of arrays)

Dynamic initialization:

int [] [] a = new int[4][5];
int [] [] b = new int[3][];
B[0] = new INT[2];
B[1] = new INT[3];
B[2] = new INT[5];
? Static initialization:
int [] [] array = {{1,2},{2,3},{3,4,5}};

Declaration and use of the java-array

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.