Array, java Array

Source: Internet
Author: User

Array, java Array

An array is the data structure of any programming language. In java, it is a set of ordered data with the same data type and a type of referenced data. Each variable in an array is called an array element. The data type of an array element can be any data type of java (including basic and reference types ). The number of elements contained in the array is called the length of the array, which is expressed by the length of the data member of the array. The length of the array is fixed after the array object is created and cannot be changed. The subscript of an array element is the offset relative to the first element of the array. Therefore, the subscript of the first element is 0, and so on. The subscript value can be an integer constant or eleven integer expressions, the value range is 0 ~ (Array length-1 ).

Arrays can be divided into one-dimensional arrays and multi-dimensional arrays.

1. One-dimensional array

  1. One-dimensional array declaration

In java, you can declare a one-dimensional array in two ways. The syntax is as follows:

Data Type [] array name;

Or

Data Type array name [];

Note: 1. The data type can be any data type, such as double, int, char, boolean, class, and String array.

2. the array name is a valid identifier.

3. square brackets [] indicate that an array is defined, rather than a common variable or object. If there are several [] square brackets, they represent several-dimensional arrays.

4. square brackets [] cannot contain numbers. Because the length of the array is not specified during declaration, it is determined by the number of memory units opened during creation.

The declared array type variables do not actually create an array, but only give the name of the array variable and the Data Type of the element. Because the array is a reference data type, therefore, the array reference value is null, indicating that no object pointing to the heap memory.

  2. Create a one-dimensional array

After declaring an array, you must allocate memory space to the array to create an array (also called an instantiated array ). Use the new Keyword to create an array. The syntax is as follows:

Array name = new array type [n];

Note: n indicates the number of elements in the created array.

The declaration and creation of arrays can also be completed in the following syntax format.

Data Type [] array name = new data type [n];

Or

Data Type array name [] = new data type [n];

When the new keyword is used to allocate memory space for an array, the system will assign a default value to each array element. The default value depends on the type of the array element. The default value of the element when an array of the reference type is created is null.

Note: once an array is created, its length cannot be changed.

 3. initialize a one-dimensional array

Array initialization means that you do not want to use the default initial value assigned by the system to assign the initial value to the array element. Array initialization includes static initialization and dynamic initialization.

    1. Static Initialization

Static initialization refers to allocating space and assigning values to array elements while defining an array. This method is usually used when there are not many array elements. The syntax is as follows:

Data Type [] array name = {element 1, element 2,..., element n };

Or

Data Type array name [] = {element 1, element 2,..., element n };

In the initialization process above, although the length of the array is not specified, the number of initial values is given. At this time, the system automatically calculates the length of the Array Based on the given initial values, allocate the corresponding space according to the data type.

    2. Dynamic Initialization

Dynamic initialization separates the declaration of the index group from the operations for allocating space to arrays and assigning values.

Example:

Public class Test {public static void main (String [] args) {int [] a = new int [5]; // initialize the array for (int I = 0; I <. length; I ++) {a [I] = I;} // print the array element for (int I = 0; I <. length; I ++) {System. out. println ("a [" + I + "] =" + a [I]) ;}}

  4. Use of one-dimensional arrays

After defining a one-dimensional array and allocating memory space to it using the new operator, you can reference each element in the array through the array name and subscript. The syntax for referencing one-dimensional array elements is as follows:

Array name [index];

Note: index indicates the subscript of an array element. It is a constant or expression of an integer. The value ranges from 0 to 0 ~ (Array length-1)

Example: randomly generate 10 1 ~ 100, and obtain the maximum and minimum values.

Public class Test {public static void main (String [] args) {int [] a = new int [10]; // for (int I = 0; I <. length; I ++) {a [I] = (int) (Math. random () * 100) + 1;} System. out. println ("the ten random numbers are:"); for (int I = 0; I <. length; I ++) {System. out. println (a [I] + "");} System. out. println (); int max = a [0]; int min = a [0]; for (int I = 1; I <. length; I ++) {if (a [I]> max) max = a [I]; if (a [I] <min) min = a [I];} system. out. println ("max =" + max); System. out. println ("min =" + min );}}

Note: The Math class is located under the java. lang Package and can be directly used without importing. This class contains methods and constants used to perform basic mathematical operations, such as Elementary Indexes, logarithm, square root, trigonometric functions, and constant PI.

Select sorting example:

Public class Test {public static void selectSort (int [] B) {for (int I = 0; I <B. length; I ++) {int k = I; // variable K is used to save the smallest subscript for (int j = I + 1; j <B. length; j ++) {if (B [k]> B [j]) {k = j ;}} if (k! = I) {int temp = B [I]; B [I] = B [k]; B [k] = temp ;}}} public static void main (String args []) {int [] a = {,}; // static initialization array selectSort (); // use array A as a method parameter and pass the handle of the array object for (int I = 0; I <. length; I ++) {System. out. print (a [I] + "");}}}

 

 

2. Multi-dimensional array

Java does not support multi-dimensional arrays, but an array element can be declared as any data type, so its element can also be an array type. Multi-dimensional arrays can be seen as arrays of arrays. Take a two-dimensional array as an example. A two-dimensional array can be considered as a special one-dimensional array. Each element of the array is a one-dimensional array.

  1. Two-dimensional array declaration

The declaration of two-dimensional arrays is the same as that of one-dimensional arrays. The syntax is as follows:

Data Type [] [] array name;

Or

Data Type array name [] [];

Note: 1. The data type can be any data type, including the basic type and reference type.

2. the array name is a valid identifier.

3. square brackets [] indicate a two-dimensional array. Square brackets represent rows, and square brackets represent columns.

4. There cannot be numbers in the square brackets [], because the length of the array is not specified during declaration.

  2. Create a two-dimensional array

A variable declared as a two-dimensional array does not actually create a two-dimensional array. to actually use a two-dimensional array, you must allocate memory space for it, that is, create a two-dimensional array (also known as instantiating a two-dimensional array ). Create the new Keyword of the Two-dimensional array. The syntax is as follows:

Array name = new array type [n] [m];

Note: 1.n indicates the number of rows in the created two-dimensional array.

2. m indicates the number of columns in the created two-dimensional array.

3. n must exist, and m may or may not exist.

The declaration and creation of two-dimensional arrays can also be completed in the following syntax format.

Data Type [] [] array name = new data type [n] [m];

Or

Data Type array name [] [] = new data type [n] [m];

  3. Two-dimensional array Initialization

   1. Static Initialization

Static initialization refers to allocating space and assigning values to array elements when defining a two-dimensional array. This method is usually used when there are not many array elements.

For static initialization, the size of each heap of a two-dimensional array is not required. The system automatically calculates the size of each heap of the array based on the number of initial values.

    2. Dynamic Initialization

Dynamic initialization means that the declaration of a two-dimensional array is separated from the assignment and assignment operations of a two-dimensional array.

  4. Use of two-dimensional arrays

Syntax:

Array name [index1] [index2];

Example:

Public class Test {public static void main (String args []) {int [] [] a = {2, 3, 7}, {, 6 }}; int [] [] B = }}; int [] [] c = new int [2] [4]; for (int I = 0; I <2; I ++) {for (int j = 0; j <4; j ++) {c [I] [j] = 0; for (int k = 0; k <3; k ++) {c [I] [j] + = a [I] [k] * B [k] [j];} System. out. println ("matrix A:"); for (int I = 0; I <. length; I ++) {for (int j = 0; j <a [I]. length; j ++) {System. out. println (a [I] [j] + "");} System. out. println ();} System. out. println ("matrix B:"); for (int I = 0; I <B. length; I ++) {for (int j = 0; j <B [I]. length; j ++) {System. out. println (B [I] [j] + "");} System. out. println (); System. out. println ("the product C matrix of matrix A and matrix B is:"); for (int I = 0; I <c. length; I ++) {for (int j = 0; j <c [I]. length; j ++) {System. out. println (c [I] [j] + "");} System. out. println ();}}}

  

 

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.