07-java one-dimensional arrays and multidimensional arrays (2)

Source: Internet
Author: User

1. Definitions of Java arrays:

1) and other programming languages, the array definition is basically the same, with 2 elements, respectively, the type is consistent (the data in the array is a type) and fixed length (the initialization has been determined by the lengths, which also means that the memory space of the group is also determined, that is, the size is unchanged);

2) in Java the array type is also a reference type, the underlying is implemented with object-oriented classes;

3) Defining arrays: Supports two formats

I. java format: type[] arrayname;

II. Traditional C language format: type arrayname[]; To meet the programming habits of C programmers

!! More recommended Java-style, because its type at a glance (type[] type), a look is known as the number of groups;

!! The above arrayname is a reference type variable, and its type is type[];

4) When defining an array variable, you cannot specify an array length, for example type[5] arrayname is not correct, because type[] is a type that cannot appear and initialize related content, and the length of the specified array is specified at initialization time!


2. Initialization of arrays:

1) type[] is also an object-oriented type, so you also need to use the new operator to create an array object (to open up memory space);

2) supports two types of initialization methods:

I. Static initialization: Does not specify a length, but displays the value of each array element specified (Java automatically determines the length of the array based on the number of initialization elements);

II. Dynamic initialization: Specify only one length, but not the value of each element, so that Java automatically assigns each element initialization value (0/NULL);

!! Can not be combined with the initialization of two initialization, or the compiler will directly error! Java separates the two types of initialization logic strictly;

3) Static initialization:

I. Format: arrayname = new type[] {ele1, ele2, Ele3 ...};

II. There is also a simplified version of static initialization, in the format: type[] arraryname = {ele1, ele2, Ele3 ...}; Can not write new type[], its style and C language like

!! However, the simplified version of the syntax can only be used to define the array variables, can not be defined and initialized separate!! Otherwise the error will be compiled! And the full version can be separated;

III. Of course, inheritance features, such as: object[] objarrary = new string[] {"xxx", "xxx" ...}; Up conversion type

5) Dynamic Initialization:

I. Format: arrayname = new Type[len];

II. This approach is often used on large arrays of length;

III. Java automatically assigns an initial value (0/null) to each element;

!! You cannot use both initialization methods (for example: int[] arr = new Int[5] {1, 2, 3, 4, 5};), will compile the error directly!!


3. Using Arrays:

1) Use the same method as the C language, using the [] index of the elements in the array;

2) Length property: Arrayname.length will return the array length, this is very common, can use this in the For loop iteration

3) foreach Iteration:

I. The new array, set iteration method introduced by Java 5 is the same as the Python iteration;

II. Format: for (Type Varname:array | collection) {...}

III. Using a temporary variable varname to accept the array | Each element in the collection, VarName is the iteration value, which is a temporary variable that cannot be used to change the value of an array element, which is equivalent to the view of an array;

Iv. type must match the type of the elements in the array/collection;

V. Example:

[Java]View Plain copy
    1. int[] arr = new int[] {1, 2, 3, 4, 5};
    2. for (int v:arr) {
    3. System.out.println (v);
    4. }

! You don't need to iterate over the iteration count variable i! Very convenient!

!! Note: It is not possible to change the elements in the array with V, because V is just a temporary variable (if you write such a statement in the For loop body, v = XXX, etc.) does not actually change the elements in the array! );


4. Multidimensional Arrays:

1) Java actually does not have multidimensional arrays, multidimensional arrays are actually simulated by one-dimensional arrays;

2) For example, a two-dimensional array, is actually a one-dimensional array, the array of each element is a one-dimensional sub-array, which formed a two-dimensional array;

3) Although the meaning of multidimensional arrays is such, the actual programming cannot be written in terms of meaning because Java specifically provides syntax for multidimensional arrays:

I. You may think type[] is a one-dimensional array type, then type[] + [] is not a one-dimensional array of type type[]? That's not the back one. [] represents the line, the front that [] represents the column?

II. This is not a problem in understanding, it is true, but Java's multidimensional array syntax is not so defined, in the above example, the preceding [] represents the row, the back of the [] represents the column, because in the programming syntax this is more in line with the general use of many groups of habits, So don't use the very awkward definition of the previous one (although the previous definition is very reasonable, but it is particularly awkward);

III. So the multidimensional array of Java type[][][] ..., the more front [] represents the higher the level, the more the back [] represents the lower the level (that is, the more front [] represents the higher dimension, the more the back [] represents the lower dimension);

4) Initialization of multidimensional arrays: As with a one-dimensional array, it is both static and dynamic

I. The principle is still the same, the two cannot be used simultaneously;

II. New type[][]. In the [] can not have anything is static, if any one [] there is a number that is dynamic;

III. Static must be in new type[][] ... After using {} to give the specified initial value, dynamic cannot specify the initial value with {}, regardless of where the array appears in [];

IV. For example:

[Java]View Plain copy
  1. int[][] a = new  int[][] {new int[ 3], new int[] { 1, 2, 3}, { 3, 4, 5}};  
  2. Int[][] B = {new int[3], new int[] {1, 2, 3}, {3, 4, 5}};
  3. Int[][] C = new int[2][];
  4. Int[][] D = new int[][3]; //Error!!  Multidimensional arrays must be dynamically initialized when high dimensions must be specified first!
  5. Int[][] e = new int[2][3];
  6. Int[][] f = new int[2][] {new int[3], new int[4]}; //Error!!  mixed with static and dynamic!

!! You can see that in any one dimension can use static simplified version of initialization, very flexible very sao;

07-java one-dimensional arrays and multidimensional arrays (2)

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.