Java self-study notes series: Array

Source: Internet
Author: User

In the world of Java object-oriented language, everything is an object. Array is no exception, and the array data structure type is also a darling in the programming world, because it can batch process common data. Next we will sort out the entire array. i. Declaration of the base 1.1 array, Declaration of the array object and initialization (taking one-dimensional array as an example): datatype [] arrays; // This is a habit of Java programmers to write // or datatype arrays []; c/C ++ uses this method to declare the array to create an array object: int [] arrays; arrays = new int [length]; // Alternatively, you can declare the array and create an object in one step: int [] arrays = new int [length]; Initialization can be divided into three forms. Default initialization: int [] arrays = new int [10]; // default initialization, which is 10 0 static initialization: assign a specific value directly to the array. Int [] arrays = {1, 3, 4, 5, 6, 7, 10} // static initialization dynamic initialization: The most common form. For (int I = 0; I <arrays. length; I ++) {// dynamic initialization, the most common arrays [I] = I * I + 8; // the concepts of expressions related to I or other expressions} arrays, the C/C ++/Java definitions are consistent, and the data types stored in the array must be consistent, and the indexes are from 0 ~ Length-1. In PHP and javascript, the array definition is relatively broad, except for the index array (and there is no hard-coded data type for the array, so various types of data can be stored ), there are also associated arrays. When accessing the index array, any language will report an array out-of-bounds error. However, the error ArrayIndexOutOfBoundsException will be reported in Java (exceptions are not expected, so the solid basic skills are the prerequisite for writing good code ). 2. In theory, N-dimensional arrays can range from 1 to infinity, but the dimension we know is no more than 4-dimensional. Therefore, the declaration of multi-dimensional arrays is as follows, in theory, object creation and usage are consistent with one-dimensional objects. One-dimensional: int [] arrays = new int [length]; two-dimensional: int [] [] arrays = new int [m] [n]; // m, n is a non-zero positive integer and is not necessarily equal. n can write no numbers, even if the two-dimensional array is not necessarily a matrix. 3D: int [] [] [] arrays = new int [l] [m] [n]; 3. array object concept analysis object reference of one-dimensional array, as shown in the following code: int [] arrays1 = new int [5]; int [] arrays2 = arrays1; // point to the same Array for (int arr: arrays1) {System. out. printf ("% 2d", arr);} System. out. println (); // The array is filled with Arrays. fill (arrays1, 60); // output after filling for (int arr: arrays1) {System. out. printf ("% 3d", arr);} System. out. println (); arrays2 [0] = 80; // after changing the index value to 0, output for (int arr: arrays1) {System. out. printf ("% 3d ", Arr);} // The execution result is as follows: 0 0 0 0 0 60 60 60 60 60 80 60 60 60 the second line of the program means, the object referenced by arrays1 is also referenced by arrays2, And the array content pointed to by the object is not changed. After 14th rows are executed, the value of arrays1 [0] changes. figure 1: object and reference name of a two-dimensional array, int [] [] arrays = new int [3] [2]; For details, see Figure 2, will it have the same effect if we create basic data classes? For example: Integer [] arrays = new Integer [3]; how many Integer objects are created in the above program fragment? 3? Error: 0. No, length = 3! In fact, as shown in figure 3, because the default value of the object is null, as shown in figure 3, if you see the following code, you will not think it is 6 objects! Integer [] [] = new int [3] [2]; directly, refer to figure 4. figure 4. array object operation 4.1 Data filling fill (); import java. util. arrays; in the first program of the three array object analysis, the code is omitted. Method 1: int [] arrays1 = {1, 2, 3, 4, 5, 6}; int [] arrays2 = new int [arrays1.length]; for (int I = 0; I <arrays1.length; I ++) {arrays2 [I] = arrays1 [I];} Method 2: Call the System function System. arraycopy (source array, source start index, target array, target revelation index, copy length) int [] arrays1 = {1, 2, 3, 4, 5, 6 }; int [] arrays2 = new int [arrays1.length]; System. arraycopy (arrays1, 0, arrays2, 0, arrays1.length); Method 3: JDK6 or above, there is a more convenient function Arrays. copyof (). In this case, you do not need to create a new array. int arrays1 [] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int arrays2 [] = Arrays. copyOf (arrays1, arrays1.length); // verify the result of array 2 for (int arr: arrays2) {System. out. printf ("% 3d", arr);} System. out. println (); arrays2 [0] = 88; // change the value of The 0th elements of array 2 // output the value of array 1, excluding the impact of object reference for (int arr: arrays1) {System. out. printf ("% 3d", arr);} // The execution result is: 1 2 3 4 5 6 7 8 9 10 1 2 3 5 6 7 8 9 10 4.3 Deep Copy first refer to the following code: package com. ivantian. array; // Car class Car {St Ring color; char size; Car (String color, char size) {this. color = color; this. size = size ;}// public class DeepCopy {public static void main (String [] args) {Car [] c1 = {new Car ("red ", 'L'), new Car ("Blue", 'M')}; Car [] c2 = new Car [c1.length]; for (int I = 0; I <c1.length; I ++) {Car car = new Car (c1 [I]. color, c1 [I]. size); c2 [I] = car;} c1 [0]. color = "yellow"; // change the C1 color value System. out. println (c2 [0]. co Lor) ;}} // The execution result is: the deep copy behavior of red means that the objects referenced by each index in c1 will be copied and assigned to each index in c2 respectively, and the result will be displayed in red. See Figure 5. Figure 5.

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.