[Javase entry series] Chapter 2 _ Array

Source: Internet
Author: User
Tags array example
Chapter 4 array -- v512 workshop Editor: Xuan Yu
Create and use Arrays Multi-dimensional array   My blog
Array element reference Copy Array V512 Studio
Declare an array type variable Array sorting Chinese Emy of Sciences

Array Overview
Summary: arrays are a combination of multiple data types to achieve unified management of these data
The array belongs to the reference type. The array type data is an object. Each element in the array is equivalent to the member variable of the object.
The elements in the array can be any data type, including basic and reference types.
Initialization: Dynamic initialization: Separate array definitions, create objects, and assign values to array elements. For example, int A [] A = new int [3] a [0] = 1
Static initialization: defines the array and assigns a space to the array elements. For example, mydate MD [] = {New mydate (1991, 1992), new mydate (,), new
Mydate (3,3, 1993 )}

 

 

 

Declare an array type variable
One-dimensional array declaration: <type> var [] or <type> [] var such as: int A [] string [] ARGs person P []
When declaring an array type variable in Java, you cannot specify the length of the array (number of elements in the array ). For example, int [3] A // is invalid.

 

 

 

Create and use arrays (For details, refer to 00:06:35-00:13:38 in this chapter)
Create an array object: New <element type> [<array length>] Because the array is also a reference type, the new method is also used to create an array object.
Default element initialization: An array is a reference type, and its elements are equivalent to member variables of the class. Therefore, once an array object is created, each element is implicitly initialized in the same way as the member variables.
Implicit initialization principle: the integer is 0, the floating point number is 0.0, The boolean type is false, and the reference type is null.

 

 

 

Array element reference
Overview: You can reference elements in an array only after creating an array object.
Format: <array name> [<element subscript>]
Description: The element subscript should be an integer expression. For example, the subscript of element a [3] B [I] C [6 * I] starts from 0, and the valid subscript value range of the array whose length is N: 0 ~ N-1
Once a Java array object is created, its length cannot be changed. If the array element is accessed out of bounds (that is, the element subscript exceeds 0 ~ N-1 range ).
The Length attribute of the array corresponds to the length of the array, that is, the number of array elements. Example: int A [] = new int [3]; system. Out. println (A. Length); // output 3

 

 

 

Multi-dimensional array (it can be understood as an array composed of several low-dimensional arrays)
Two-dimensional array example: int A [] [] = {1, 2}, {3, 4,}, {5, 6, 7 }};
Multi-dimensional array features: the Declaration and initialization of multi-dimensional arrays in Java should be performed in the order from high-dimensional to low-dimensional.
Multi-dimensional arrays in Java do not have to be in the Rule matrix form.
Multi-dimensional array initialization: static initialization: int A [3] [2] = {1, 2}, {2, 3}, {4, 5} // invalid. Do not assign a number to the array during static Initialization
Dynamic initialization:
Multi-dimensional array example: int [] [] A = new int [3] [4] // valid
Int [] [] T = new int [3] [] // valid
Int [] [] B = new int [] [4] // invalid
Int A [] [] = {1, 2}, {3, 4, 5}, {6, 7, 8, 9} // A [2] [1] = 7
First, locate the element with the subscript of 2 in the one-dimensional array, that is, 3rd elements, that is, {6, 7, 8, 9 }. Then, locate the 2nd elements in the two-dimensional array, that is, the value 7 in the array.

 

 

 

Copy Array
Overview: The arraycopy () method of the Java. Lang. system class provides the array element copying function to copy the values of multiple elements in an array to another array in batches.
Match the two array types. Arraycopy () is a static method that can be used directly by class name.
Example: int source [] = {1, 2, 3, 4, 5}; // source array // copy the three elements in the source array starting from subscript 0 to the destination Array
Int Dest [] = {10, 9, 8, 7, 6, 5}; // target array // The destination array is stored at the position of subscript 0.
System. arraycopy (source, 0, DEST,); // The Last Dest [] element is}

 

 

 

Array sorting
Overview: The arrays class defined in the JDK java. util package provides multiple array sorting functions.
Example: int [] A = {6, 3, 5, 2,-8, 9, 1}; // create a sample Array
Arrays. Sort (a); // sort the array // The following is the output of the sorted array element.
For (INT I = 0; I <A. length; I ++) {system. Out. Print (A [I] + "\ t ");}
Note: Elements in the array are sorted from small to large by default. To output from large to small, you just need to change to print (A [A. length-1-i] + "\ t ").

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.