Java Experience--array 2__java

Source: Internet
Author: User

1. Foreach Loop

when you iterate through the array and collection elements using a foreach loop, you do not need to get the length of the array and the collection to access the array elements and the collection elements based on the index, and the system automatically traverses each element of the array and collection.

Μ Format:
For (type variable name: Array name | collection name) {
Variables are automatically iterated to access each element ...
}

Μ Note: If there are n elements in the array, then the loop loops n times; Each loop assigns an element of an array to the variable, and the loop body operates on the variable.

For example:

string[] Names ={"Georgianna", "Jen", "Simon"};

For (String name:names) {

System.out.println (name);

}

Note:

U iterate through an array element with a foreach loop without having to get the array length or access the array elements based on the index.

You do not need to cycle the conditions, do not need to loop iterative statements, these parts are completed by the system. Each element of an automatic iteration group, when each element is iterated once, the Foreach loop automatically ends.

when using a foreach loop, you usually do not assign values to the loop variable, although there is no syntax error, but it is very easy to cause errors.

when using foreach to iterate over an array element, the loop variable is equivalent to a temporary variable, and the system assigns the array element to the temporary variable in turn, but it is not an array element. Changing the value of a loop variable does not change the value of the element. Therefore, you cannot use this foreach loop if you want to change the value of an array element.

2. Multidimensional arrays can be understood as arrays of several low dimensional arrays, for example, multiple one-dimensional arrays can be grouped into "two-dimensional arrays", and multiple two-dimensional arrays form three-dimensional arrays .

to access an element in a two-dimensional array, you need to know the position of the element (the first few rows, the first columns)

3. Two-d array declarations

format: type name variable name []; or type name [[] variable name;

For example, int[][] s;  Student s[][];

(the first corner mark (that is, the first []) represents the row, and the second represents the column. )

Note: Declaring an array type variable, the runtime system allocates reference space only for reference variables, does not create objects, and does not allocate space for array elements, so you cannot use any of the array elements. (As with a one-dimensional array)

4. Creation and initialization of two-D array objects

Μ Static Initialization
Format: Type [] [] array name ={{No. 0 Row Initial value},..., {n-1 row initial value}};
For example, int[][]s={{67,89,53},{77,98,68},{57,66,73},{80,88,94},{84,92,90}};

dynamic initialization of Μ

U (first) allocate space directly for each dimension
For example, int [] [] s=new int[5][3];

U (the second) starts from the highest dimension (the number of rows) (must start from the highest dimension) and allocates space for each dimension respectively. (Specify only the number of rows, not the number of columns)

For example, int[][] s=new int[3][];
S[0]=new Int[3];

s[1]=new int[3];

s[2]=new int[3];

can't write like this:

s[0]={1,2,3};

s[1]={1,2,3};

s[2]={1,2,3};

but you can write this:

s[0]=new int[3];

s[0][0]=1;

s[0] [1]=2;

s[0] [2]=3;

You can also write this:

s[0]=new int[]{1,2,3};

s[1]= New int[]{1,2,3};

s[2]= New int[]{1,2,3};

5. Note: In Java, you allow the number of elements in a two-dimensional array to be different, that is, the number of columns per row can be different.

For example, static initialization: int[][]={{1,2},{1,2,3},{1,2,3,4}}

For example, dynamic initialization: int[][] s=new int[3][];

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

6. Note: The length property of the two-dimensional array s can be used to obtain the size of the two-dimensional array high dimensional (the number of rows), and the S[i].length property to get the number of columns per row of the two-dimensional array, that is, the number of elements per row.

for (int i=0;i<s.length;i++) {

for (int j=0;j<s[i].length;j++)

Sum+=s[i][j];

  }

6. The arrays class, defined in the JDK Java.util package, provides a variety of array manipulation methods that enable sorting, padding, conversion, enhanced retrieval, and depth comparison of the elements of the arrays, all of which are static.

sorting method of ¯ array elements
Prototype: public static void Sort (<type>[] a);
For example, int[]a={4,12,0,-5,45,-56};
Arrays.sort (a);

Example 6.7 array of sorts.

Note: The ordering of array elements usually refers to the one-dimensional numeric array elements sorted in ascending order, occasionally involving the ordering of one-dimensional string arrays, and the array ordering of multidimensional and other reference types is of little practical significance.

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.