Algorithm (4th edition) -1.1.5 array

Source: Internet
Author: User

Summary: This section describes the meaning of the array, the three steps to create the array, the typical array processing code, the aliases, and the contents of the two-dimensional array.

Focus:

1. When creating an array, you need to specify the length of the array (the number of elements). The reason we need to explicitly create an array at run time is because the Java compiler does not know at compile time how much space should be reserved for the array (which is possible for the original type).

2.

Doublenewdouble[N];  for (int i = 0; i < N; i++)     = 0.0;

The For statement initializes the n elements of the array, placing their values at 0.0. When using arrays in your code, be sure to declare, create, and initialize the array in turn. Ignoring any of these steps is a common programming error.

However, the for statement does not need to be initialized in the actual test.

3.Java automatically checks for bounds-if you create an array of size n, but use an index that is less than 0 or greater than N-1 to access it, the program terminates because the runtime throws an Arrayoutofboundsexception exception.

4. Familiarize yourself with the typical array processing code (P11-P12):

· Find the largest element in an array

· Computes the average of an array element

· Copying an array

· Reverse the order of array elements

· Matrix multiplication (Phalanx)-a[][] * b[][] = c[][]

5. The array name represents the entire array-if we assign an array variable to another variable, then two variables will point to the same array. If you want to copy an array, you should declare, create, and initialize a new array, and then copy the element values from the original array to the new array, as shown in the third example above.

6. When creating a two-dimensional array, specify the number of rows and the number of columns in square brackets after the type name, for example:

Double New Double M [N];

Algorithm (4th edition) -1.1.5 array

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.