Java Array Detailed

Source: Internet
Author: User

In the Java language, arrays are one of the simplest types of composite data. An array is a collection of ordered data, each of which has the same data type, and can uniquely determine the elements in an array with a uniform array name and subscript. The array has a one-dimensional array and a multidimensional array.
★ One dimensional array
1. Definition of one-dimensional array

Type arrayname[];
Type can be any type of data in Java, including simple types and compound types.
For example:
int intarray[];
Date datearray[];


2. Initialization of one-dimensional arrays

◇ Static Initialization
int intarray[]={1,2,3,4};
String stringarray[]={"abc", "How" and "You"};

◇ Dynamic Initialization
1 Arrays of simple types
int intarray[];
Intarray = new Int[5];

2 array of compound types
String stringarray[];
String Stringarray = new string[3];/* Each element in the array as a reference
Space (32 bits) */
stringarray[0]= New String ("how");//Open space for first array element
stringarray[1]= New String ("are");//Open space for second array element
stringarray[2]= New String ("You");//Open space for third array element

3. A reference to a one-dimensional array element

The array elements are referenced in the following ways:
Arrayname[index]

Index is an array subscript, which can be an integer constant or an expression, with the subscript starting at 0. Each array has an attribute length indicating its length, for example: Intarray.length indicates the length of the array intarray.

★ Multidimensional Array

In the Java language, a multidimensional array is treated as an array of arrays.

1. Definition of two-dimensional arrays

Type arrayname[] [];
Type [] []arrayname;

2. Initialization of two-dimensional arrays

◇ Static Initialization
int intarray[] []={{1,2},{2,3},{3,4,5}};

In the Java language, the array space is not continuously allocated because it is considered an array of arrays, so it is not required to have the same size for each dimension of the two-dimensional array.

◇ Dynamic Initialization
1) Allocate space directly for each dimension, the format is as follows:
Arrayname = new Type[arraylength1][arraylength2];
int a[] [] = new INT[2][3];

2 start from the highest dimension and allocate space for each dimension respectively:
Arrayname = new type[arraylength1][];
Arrayname[0] = new TYPE[ARRAYLENGTH20];
ARRAYNAME[1] = new TYPE[ARRAYLENGTH21];
...
Arrayname[arraylength1-1] = new TYPE[ARRAYLENGTH2N];

3) Example:
The dynamic initialization of a two-dimensional simple data type array is as follows,
int a[] [] = new int[2][];
A[0] = new INT[3];
A[1] = new INT[5];

For arrays of two-dimensional composite data types, you must first allocate the reference space for the highest dimension and then the lower-dimensional space allocation in sequence.
Also, you must allocate space for each array element individually.

For example:
String s[] [] = new string[2][];
s[0]= new string[2];//Allocates reference space for the highest dimension
s[1]= New string[2]; Assigning reference space to the highest dimension
s[0][0]= New String ("Goo

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.