Java Fundamentals Four (arrays in java,c++)

Source: Internet
Author: User
Tags array definition

The memory allocation of the array in Java is in the heap, must be allocated with new, and C + + inside the stack is allocated (in addition to using the pointer new out of the array), the definition will be automatically assigned.

1. Arrays in Java

(1) An array is not a collection, it can only hold references to multiple primitive types or objects of the same type. An array saves only the object's references, not the object itself. Two forms of an array declaration: One, int[] arr;  II, int arr[]; It is recommended to use the former, which is an int array object instead of an int primitive type.

(2) The array itself is an object, and the object in Java is in the heap, so the array object itself is in the heap, regardless of whether it holds the original type or other object types.

(3) It is always illegal to include the array length in an array declaration! such as: Int[5] arr;. Because no object is instantiated at the time of the Declaration, only the JVM allocates space when instantiating the array object, which is related to length.

(4) The length must be specified when the array is constructed, because the JVM needs to know how much space to allocate on the heap. Example: int[] arr = new INT[5];

(5) The construction of a one-dimensional array.  form: string[] sa = new string[5];  Or split into two sentences: string[] sa; SA = new String[5];

(6) The default value of the primitive type array element. For primitive type arrays, the JVM automatically initializes them when the new construct is complete without initialization. Default value: Byte, short, int,long--0 float--0.0f double--0.0 boolean--false char--' "u0000 '. (whether the array is a member variable or a local variable)

(7) An array of object types, although initialized by default, does not call its constructor. (called in C + +) i.e.: car[] MyCar = new CAR[10]; only one MyCar Array object is created! There is no instance of the car object created!

(8) Construction of multidimensional arrays. Float[][] ratings = new float[9][]; The length of the first dimension must be given, and the rest may not be written, because the JVM only needs to know the length of the object assigned to the variable ratings.

(9) The range of the array index. The index of each element in the array starts at 0, to Length-1. Each array object has a length property that holds the size of the array object. (Note that it is distinguished from the length () method of the String object)

Java has an array subscript check that, when accessed beyond the index range, produces a arrayindexoutofboundsexception run-time exception. Note that this subscript check does not go into line at compile time, but at run time!  i.e. int[] arr = new INT[10]; ARR[100] = 100; Such obvious errors can be compiled, but thrown at run time!

The array in Java can store both the base value type and the object. An array of objects and primitive data types are almost identical to the method used, with the only difference being that the array holds references and the original data type arrays contain specific values. When discussing questions about arrays, be sure to first determine whether the array stores the base value type or the object. especially when debugging a program, be aware of this aspect.

Display columns:

One-dimensional arrays

1) int[] A; Declaration, not initialized

2) int[] a=new int[5]; Initialize to default value, int type is 0

3) int[] a={1,2,3,4,5}; Initialize to the given value

4) int[] A=new int[]{1,2,3,4,5}; With (3) int[] a=new int[5]{1,2,3,4,5}; Error, cannot define a dimension expression if an array initialization operation is provided

5) int[] A; A=new Int[5]; Right, same as (2)

Int[] A; a={1,2,3,4,5}; Error, array constants can only be used in initialization operations, such as (3)

6) int a[]; A[0]=1; Error because the array is not initialized and cannot be assigned a value of a[1]=2;

Two-dimensional arrays

1) int[][] A; Declaration, not initialized

2) int[][] a=new int[2][3]; Initialize to default value, int type is 0

3) int[][] a={{1,2},{2,3},{3,4}}; Initialized to the given value int[][] a={{1,2},{2,3},{3,4,5}}; no mistake, the array space is not contiguous, so the size of each dimension is not required

4) int[][] A=new int[2][]; A[0]=new Int[3]; A[0] is actually an array a[1]=new int[4]; The size of each dimension can be different;

5) int[][] A=new int[][]{{1,2},{2,3},{3,4,5}}; With (3) int[] a=new int[5]{{1,2},{2,3},{3,4,5}}; error, cannot define a dimension expression if an array initialization operation is provided

Int[][] A=new int[2][]; a[0]={1,2,3,4,5}; Error, array constants can only be used in initialization operations

6) int[][] A=new int[2][]; A[0][1]=1; Error, second dimension not initialized, cannot assign value, Java.lang.NullPointerException exception

Summary: 1. Two dimension is an array of arrays, the size of the array does not require the same

2. either one-dimensional or two-dimensional, before use (assignment, access) must be initialized, can be initialized by default with new, or can be initialized with array constants


1. Dynamic initialization: The array definition is separate from the assignment of the space and assignment of the arrays; (generally using pointers to implement dynamic arrays, returning the first address of the array)

2. Static initialization: Allocates space and assigns values to the array elements while defining the numbers;

3. Default initialization: An array is a reference type and its elements are equivalent to the member variables of the class, so after the array allocates space, each element is also initialized by the hermit according to the rules of the member variable.

1. Arrays in C + +

The array definition of C + + can be divided into two types, one is allocated on the stack space, and one is allocated on the heap space.

such as int a[3] This form of definition, the allocation of memory in the stack space to complete;

int *a = new Int[3], this form of memory allocation is done in heap space.

Display columns:

(1) Definition and initialization of one-dimensional arrays
1) Defining a one-dimensional array
int a[3];
The definition represents an array of integers with 3 elements and subscript [0],[1],[2] respectively.
2) One-dimensional array initialization
It can be initialized in several ways.
① assigns an initial value to an array element, respectively, when it is defined.
int a[3]={0,1,2};
② only assigns values to a subset of the elements.
int a[3]={0,1};
This means that only the first two elements are assigned an initial value, and the following element values default to 0.
③ the array length can be specified without assigning an initial value to all array elements.
int a[3]={0,1,2};
can be written
int a[]={0,1,2};
In the second notation, with 3 elements in the curly braces, the system automatically defines the length of the A array as 3. However, if the length of the defined array is not the same as the number of initial values provided, the array length cannot be omitted.

④ pointer Assignment array

int *a = new Int[5];
(2) Definition and initialization of two-dimensional arrays
1) Define a two-dimensional array
int a[3][4];
Defines an integer array that represents a 3x4 (3 rows and 4 columns). You can think of this two-dimensional array as:
|---a[0]:a[0][0],a[0][1],a[0][2],a[0][3]
A |--a[1]: a[1][0],a[1][1],a[1][2],a[1][3]
|--A[2]: a[2][0],a[2][1],a[2][2],a[2][3]
In C + +, the order in which the elements of a two-dimensional array is arranged is to store the first row of elements in memory sequentially, in the second row of elements, and down in turn.
2) Two-dimensional array initialization
It can be initialized in several ways.
① Branch assigns an initial value to a two-dimensional array.
int a[3][4]={{0,1,2,3},{4,5,6,7},{8,9,10,11}};
② writes all the data in a curly brace, assigning an initial value to each element in the order in which the array is arranged.
int a[3][4]={0,1,2,3,4,5,6,7,8,9,10,11};
③ only assigns initial values to some elements.
int a[3][4]={{1},{2},{4}};
1 0 0 0
2 0 0 0
4 0 0 0
int a[3][4]={{1},{0,2},{0,0,4}};
1 0 0 0
0 2 0 0
0 0 4 0
int a[3][4]={{1},{3,2}};
1 0 0 0
3 2 0 0
0 0 0 0
int a[3][4]={{1},{},{9}};
1 0 0 0
0 0 0 0
9 0 0 0
④ If an initial value is assigned to all elements, the first-dimension length can be unspecified when the array is defined, but the second-dimensional length must be specified.
int a[3][4]={0,1,2,3,4,5,6,7,8,9,10,11};
can be written
int a[][4]={0,1,2,3,4,5,6,7,8,9,10,11};
In the definition, you can also assign initial values to some elements and omit the length of the first dimension, but the initial value should be assigned to the branch.
int a[][4]={{0,0,1},{},{0,9}};
0 0 1 0
0 0 0 0
0 9 0 0

⑤ pointer Assignment array

int **a = new int *[5];

for (int i =0;i<5;i++)

{

A[i] = new INT[10];

}

Allocates the 5*10 int space.


Reference: http://www.cnblogs.com/Yogurshine/archive/2012/12/29/2839238.html

http://blog.csdn.net/SpeedMe/article/details/22925977

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java Fundamentals Four (arrays in java,c++)

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.