Java -- array, java Array

Source: Internet
Author: User
Tags array definition

Java -- array, java Array

An array is a combination of multiple arrays of the same data type. The data type is any data type.

Array variables are reference type variables. Arrays can be used as objects. Each element in the array is equivalent to a member variable of the object. Therefore, array elements can be initialized by default. (Blog java -- Variable Classification has descriptions about member variable initialization ).

One-dimensional array
Statement

Type var []; or type [] var

Initialization

Dynamic initialization: separate definitions from array element assignment and assignment

int a[];a=new int[3];a[0]=0;a[1]=1;a[2]=2;

Static initialization: Allocate space and assign values to array elements while defining

int a[]={1,2,3};
Memory Analysis
class TestArray{public static void main(String arg[]){int[] s;s=new int[5];for(int i=0;i<5;i++){s[i]=i;}}}

When the above program is executed to the end, the memory should be like this:

Two-dimensional array

A two-dimensional array can be seen as an array with arrays as elements.

Initialization

Dynamic initialization (note: the Declaration and creation of multi-dimensional arrays in java are performed in the order from high-dimensional to low-dimensional)

int a[][]=new int[3][];a[0]=new int[2];a[1]=new int[4];a[2]=new int[3];

Static Initialization

int a[][]={{1,2},{2,3},{3,4}};
Memory analysis:
class TestArr{public static void main(String arg[]){int s[][]=new int[3][];s[0]=new int[2];s[1]=new int[4];s[2]=new int[3];for(int i=0;i<a.length;i++){for(int j=0;j<s[i].length;j++){s[i][j]=i;}}}}

When the above program is executed to the end, the memory should be like this


Summary

1. Elements of a one-dimensional array in java are variables of the same data type, while a two-dimensional array combines multiple one-dimensional arrays into an array. Therefore, it is very important to understand one-dimensional arrays. 2. An array is a reference variable and can be used as an object. Its element is a member variable.


Java array reference

Because the array is an object ..

In java, parameter passing is reference passing ..

Pass
Public B (String [] sss, String s ){
Sss [0] = "bbbbb ";
Sss [1] = "ccccc ";
S = "2222 ";
}
The sss here is a reference to that array object .. Pointer ..
Sss [0] = "bbbbb ";
Sss [1] = "ccccc ";
This is equivalent to calling the method of this array object ..
This is equivalent to processing this object ..
So the value of this object has changed ..
However, the reference String [] a = {null, "aaaaa"}; a still points to that object .. Therefore, the output result is the result after the change ..
Assume that you
Public B (String [] sss, String s ){
Sss [0] = "bbbbb ";
Sss [1] = "ccccc ";
S = "2222 ";
}
Change
Public B (String [] sss, String s ){
String [] strs = new String [2];
Strs [0] = "bbbbb ";
Strs [1] = "ccccc ";
Sss = strs;
S = "2222 ";
}
In this case, the value of the object will not be changed ..
Just re-assign the value to the reference of sss... let him point to another object ..
In this case, the reference still points to the object. The value of the object will not change ..

Why is the value of the String object not changed ..
Same as above ..
Just re-assigned the value of s reference. Let him point to a new String object ..
String aa = "111111"; aa still points to the String object with a value of 111111 ..

Java array definition and usage

An array is a set of ordered data. Each element in an array has the same array name and subscript to uniquely identify the elements in the array.

§ 5. 1 one-dimensional array

I. Definition of one-dimensional arrays

Type arrayName [];

The type can be any data type in Java, including a simple type combination type. The array name arrayName is a valid identifier. [] indicates that the variable is an array type variable. For example:

Int intArray [];

Declares an integer array. Each element in the array is an integer. Unlike C and C ++, Java does not allocate memory for array elements in array definition. Therefore, [] does not specify the number of elements in the array, that is, the length of the array, in addition, an array defined above cannot access any of its elements. We must allocate memory space for it. The new operator is used in the following format:

ArrayName = new type [arraySize];

ArraySize indicates the length of the array. For example:

IntArray = new int [3];

Allocate the memory space occupied by three int Integers to an integer array.

Generally, these two parts can be combined in the following format:

Type arrayName = new type [arraySize];

For example:

Int intArray = new int [3];

2. Reference of one-dimensional array elements

Defines an array and allocates memory space for it with the new operator. Then, each element in the array can be referenced. The reference method of array elements is as follows:

ArrayName [index]

Here, index is an array subscript, which can be an integer constant or expression. For example, a [3], B [I] (I is an integer), and c [6 * I. The subscript starts from 0 until the length of the array is reduced by 1. For the in-tArray number in the preceding example, it has three elements:

IntArray [0], intArray [1], intArray [2]. Note: There is no intArray [3].

In addition, unlike C and C ++, Java checks the array elements out of bounds to ensure security. At the same time, each array has a property length to specify its length. For example, intArray. length indicates the length of the array intArray.

Example 5.1

Public class ArrayTest {
Public static void main (String args []) {
Int I;
Int a [] = newint [5];
For (I = 0; I <5; I ++)
A [I] = I;
For (I = a. length-1; I> = 0; I --)
System. out. println ("a [" + I + "] =" + a [I]);
}
}

The running result is as follows:

C: \> java ArrayTest

A [4] = 4
A [3] = 3
A [2] = 2
A [1] = 1
A [0] = 0

This program assigns values to each element in the array and then outputs the values in reverse order.

3. Initialization of one-dimensional arrays

You can assign values to array elements according to the preceding example. You can also Initialize an array.

For example:

Int a [] = {1, 2, 3, 4, 5 };

Use commas (,) to separate the elements of an array. The system automatically allocates space for the array.

Different from C, Java does not require the array to be static ).

4. Example of a one-dimensional array program:

Example 5.2Fibonacci Series

The Fibonacci series is defined:

F1 = F2 = 1, Fn = Fn-1 + Fn-2 (n> = 3)

Public classFibonacci {
Public static void main (String args []) {
Int I;
Int f [] = new int [10];
F [0] = f [1] = 1;
For (I = 2; I <...... remaining full text>

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.