Java programming notes 2 Java Array

Source: Internet
Author: User

In Java, the array is regarded as an object, and length is the attribute of the array rather than the method.

1. One-dimensional array
Definition: datatype [] arrayname; or
Datatype arrayname []; the first type of Java habits is recommended.
The array definition only declares the variables of the array type. In fact, the array does not exist in the memory. To use the array, you need to apply for space for the array:
Arrayname = new datatype [arraysize]
Example:
Int [] intarray;
Intarray = new int [5];
It can also be completed in one step
Int intarray [] = new int [5];

Default Value during initialization
Byte short int long 0
Float double 0.0
Char \ 0 (Space)
Boolean false
Object Type null

Two-dimensional array (array)
Definition:
Datatype [] [] arrayname; or
Datatype arrayname [] [];

Create:
Int array [] [] = new int [4] [6]; or

// Contains two one-dimensional arrays
Int array [] [] = new int [2] [];
Array [0] = new int [4];
Array [1] = new int [5];
Note that the second dimension is different.

Initialization:
Direct initialization:
Int array [] [] = {1, 2, 3}, {5, 6}, {6, 9 }};
Note that array [1] has only two numbers, 5 and 6, and is not the default value.

Initialize as an array
Int array [2] []; // defines the high dimension as 2.

Int row0 [] = {1 };
Int row1 [] = {2, 3, 5 };
// The array in Java is regarded as an object, and the object name can be assigned a value. Note that array [0] is the name of a one-dimensional array.
Array [0] = row0;
Array [1] = row1;

Array copy arraycopy Method
System. arraycopy (Object SRC, int src_position, object DST, int dst_position, int length );
Copy the length element from src_position of SRC to dst_position of DST.

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.