I remember when I wrote JavaScript notes:ProgramIs composed of data and operations.. Therefore, the study of data storage and reading methods is an important way to master the essence of language. In my previous articleArticleI want to review some knowledge about data storage and complex data storage. I personally think a good programmer should have four main indicators:First, project experience, second, program optimization capability, third, good design philosophy, and fourth, ability to quickly and accurately locate program bugs. Project experience is not mentioned. This requires accumulation, and other capabilities can be continuously strengthened through learning. The advantages and disadvantages of data storage in languages are an important indicator of the level of your optimization program,You want your programs to get faster and faster. What data models are used to quickly store data and quickly retrieve the stored data is the essence of program optimization.Therefore, it is very important to learn how to store data, especially complex data.
(I will never learn and improve myself. I am not a tech enthusiast, but I am a person who wants to make my career better, and I want to create innovative things that really make sense one day, so I am constantly motivated and always ready to seize the opportunity and Inspiration day, because I believe that the inspiration and opportunity for innovation comes from the process of your constant preparation of inspiration and opportunity arrival .)
Well, I started to do practical work without talking about big things. I don't want to innovate. I just want to open my mind when writing a program. I don't want to write arraylist as long as it is an array. When I encounter a key-value pair, it is hashmap. Maybe we have a better choice, but you must know what a better choice is.
The first truth of Java:In Java, the basic type is object.. An important indicator of better Java than C:Java provides a large number of collection classes for object operations. Most of these collection classes are in the Java. util package..
However, the first data structure used to store complex data is not list, set or map, but an array ). Let's start with the array.
How to define arrays
If an interviewer asks you to answer this question, can you give it a full explanation? Maybe many children's shoes are acceptable, but I have not answered them comprehensively several times. Let's take a look at the followingCode:
Package Cn.com. sxia;
Import Java. util. arraylist;
Import Java. util. List;
Public Class Arraydefine {
Public Static Void Main (string [] ARGs ){
// Note 1
Int [] A1;
Int A2 [];
// Note 2
Int [] A3 = {1, 2, 3, 4, 5 };
Int A4 [] = {6, 7, 8, 9, 0 };
// Note 3
Int [] A5 = New Int [3];
Int A6 [] = New Int [3];
// Note 4
Integer [] B1;
Integer B2 [];
Integer [] B3 = New Integer [3];
Integer B4 [] = New Integer [3];
// Note 5
Integer [] B5 = {1, 2, 3 };
Integer [] B6 = { New INTEGER (4 ), New INTEGER (5 ), New INTEGER (6 )};
System. Out. println ("b5.length:" + b5.length );
System. Out. println ("b6.length:" + b6.length );
// Note 6
Integer [] B7 = New Integer [] { New INTEGER (4 ), New INTEGER (5 ),
New INTEGER (6 )};
System. Out. println ("b7.length:" + b7.length );
}
}
The result is as follows:
B5.length: 3
B6.length: 3
B7.length: 3
Next I will explain the annotations one by one:
Note 1:Java provides two groups of arrays, which are equivalent, while int A2 [] is the style of C and C ++. Defining an array in Java only indicates that a variable has obtained an array reference, but the array is not allocated any space at this time, to obtain the corresponding memory space for the array, you must initialize the array.
Note 2: If we know that our data is to be put into an array, this data is definitely definite, so the method of comment 2 is a simple array initialization method:Use curly brackets to initialize an array..
Note 3: This instance gives us another option: When we only know the number of arrays and do not know the array content, how should we define the array? At this time, the Length attribute of the array stores the length of the array. length is perhaps the most commonly used attribute in the data structure of the array. It is appropriate to write down its value with a local variable, instead of re-calculating the length value through arrays each time, it is an effective way to improve program efficiency.
Note 4: Here, I changed the data type to an object. The usage is the same as that of the basic type.
Note 5: Initialize with curly bracketsObjectWe can directly use int type initialization for integer type. Java will automatically convert int to this integer object.
Note 6: Another method for defining object arrays is provided here. The effect is similar to that of the code below annotation 5, but the code below annotation 6 is flexible. For example, we replace integer with object, then this array becomes a general array. This tips is too child, so I won't go into depth here.
Although the basic types of array storage are similar to those of objects in terms of code representation, they are essentially different:The array of objects stores references, while the array of basic types directly stores the values of basic types..
In Java, we often ignore arrays. This is an extremely bad habit.Programming LanguageIt is sometimes an excellent complex data storage structure. We compare arrays with the arraylist class in Java. arrays have the following advantages:
- High Efficiency: Arrays are the most efficient way to store and reference random access objects in Java. arrays use a simple linear structure, which enables fast thread access. In addition, the thread length is fixed, which saves the performance overhead caused by the dynamic length, which is also an important reason for the fast array. Finally, the data stored in the array is of the same type. Therefore, when using the array, the checksum and conversion of the data type are not required, in this way, the array efficiency is much more efficient than that of the Collection class that can store any type of data.
- Arrays have the ability to store basic types.: The collection class in Java is designed for Object Storage, and any data type of arrays can be used as its storage content.
However, the Java util package still provides the arrays tool class for Array Operations, which is convenient to use statically, but I personally think this class is of little value, many features can be better written by ourselves. Although arrays is not my favorite, arraycopy in the system class is very fond of me. Before reading the code, let's take a look at the explanation of the arraycopy method in the JDK document:
Arraycopy
Public Static Void Arraycopy (Object SRC,
Int Srcpos,
Object DEST,
Int Destpos,
Int Length. From the source array referenced by Src to the target array referenced by DEST, A subsequence of the array component is copied. The number of the copied component is equal to the length parameter. Components located between srcpos and srcpos + Length-1 in the source array are copied to destpos and destpos + Length-1 in the target array respectively.
If the SRC and DEST parameters reference the same Array object, the execution process of the replication is like copying the srcpos component to the srcpos + Length-1 position to a temporary array with the length component, then copy the contents of the temporary array to the destpos of the target array to the same position as destpos + Length-1.
If DEST is Null , Then an nullpointerexception is thrown.
If SRC is Null , The nullpointerexception is thrown, and the target array is not modified.
Otherwise, if any of the following conditions is true, an arraystoreexception is thrown and the target array is not modified:
The SRC parameter indicates a non-array object.
The Dest parameter indicates a non-array object.
The SRC and DEST parameters refer to arrays whose component types are different basic types.
The SRC Parameter refers to an array with the basic component type and the Dest parameter refers to an array with the referenced component type.
The SRC Parameter refers to an array with reference component types and the Dest parameter refers to an array with basic component types.
Otherwise, if any of the following conditions is true, the indexoutofboundsexception exception is thrown and the target array is not modified:
The srcpos parameter is negative.
The destpos parameter is negative.
The length parameter is negative.
Srcpos + length is greater than SRC. length, that is, the length of the source array.
Destpos + length is greater than DeST. length, that is, the length of the target array.
Otherwise, if the actual component from srcpos to srcpos + Length-1 in the source array cannot be converted to the component type of the target array through allocation conversion, an arraystoreexception exception is thrown. In this case, set K to a minimum non-negative integer smaller than the length, so that SRC [srcpos + k] cannot be converted to the component type of the target array. When an exception is thrown, the source array component from the srcpos to the srcpos + k-1 position has been copied to the destpos to the destpos + k-1 position in the target array, while other locations in the target array are not modified. (Because of the limitations described in detail, this section can only be effectively applied when both arrays have referenced component types .)
Parameters:
Src-source array.
Srcpos-start position in the source array.
DeST-target array.
Destpos-start position in the target data.
Length-the number of array elements to be copied.
Throw:
Indexoutofboundsexception-if copied, access to data outside the array range is allowed.
Arraystoreexception-if the types do not match, elements in the SRC array cannot be stored in the Dest array.
Nullpointerexception-If SRC or DEST is Null .
The Code is as follows:
Package Cn.com. sxia;
Import Java. util. arrays;
Public Class Arrayscopy {
Public Static Void Main (string [] ARGs ){
/* Basic Data Type */
System. out. println ("====================== basic data type ============== ==================== ");
Int Arr1 [] = New Int [5];
Int Arr2 [] = New Int [9];
// Use of some arrays tool classes
Arrays. Fill (arr1, 7 );
System. Out. println (arrays. tostring (arr1) + "the length of arr1 is:" + arr1.length );
Arrays. Fill (arr2, 9 );
System. Out. println (arrays. tostring (arr2) + "the length of arr2 is:" + arr2.length );
// Copy Array
System. arraycopy (arr1, 0, arr2, 0, arr1.length );
System. Out. println ("new array:" + arrays. tostring (arr2) + "the length of arr2 is:" + arr2.length );
/* Object operations */
System. out. println ("================================ object operation ========== ==================== ");
Integer [] arrobj1 = New Integer [5];
Integer [] arrobj2 = New Integer [9];
Arrays. Fill (arrobj1, New INTEGER (7 ));
System. Out. println (arrays. tostring (arrobj1) + "the length of arrobj1 is:" + arrobj1.length );
Arrays. Fill (arrobj2, New INTEGER (9 ));
System. Out. println (arrays. tostring (arrobj2) + "the length of arrobj2 is:" + arrobj2.length );
// Copy Array
System. arraycopy (arrobj1, 0, arrobj2, 0, arrobj1.length );
System. Out. println ("new array:" + arrays. tostring (arrobj2) + "the length of arrobj2 is:" + arrobj2.length );
}
}
The running result is as follows:
======================================== Basic Data Type ==================
[7, 7, 7, 7, 7] The length of arr1 is: 5
[9, 9, 9, 9, 9, 9, 9, 9] The length of arr2 is: 9
New array: [7, 7, 7, 7, 7, 9, 9, 9, 9] The length of arr2 is: 9
====================================== Object operation ==================== ==================
[7, 7, 7, 7, 7] The length of arrobj1 is: 5
[9, 9, 9, 9, 9, 9, 9, 9] The length of arrobj2 is: 9
New array: [7, 7, 7, 7, 7, 9, 9, 9, 9] The length of arrobj2 is: 9
In the code, I demonstrated some functions of the arrays class. arrays are often used because the array size is fixed at the beginning, so we have to re-copy the array, system. the arraycopy method provides a very simple solution, but this method is also problematic. From the code, we can think that the array of the arraycopy method of system can be copied, however, copying an object array is a little different. Copying an object copies a reference of an object instead of the object itself. This copy is called shallow copy ). The problem with the method is that this is caused by the shallow copy. Next I will talk about the shallow copy and its corresponding deep copy. The definition of shortest copy and deep copy is as follows:
Shortest copyFor example, if object A is copied to object B, but object B is copied to object A, if object a still has an array pointing to another object or is referenced, object B does not copy the internal information but points to the information referenced by object.
Deep copy: The Example of copying object A to object B. copying object B to object A contains all the information of object A, including object references in object.
I may see that the above explanation is not clear about the differences between the copy and the copy. I will use the image below to show the differences between them. The first is object, A contains two internal objects innera and innerb, such:
If you copy object A to object B, but object B does not copy the innera object and innerb object internally, object B still points to the original innera object and innerb object, such:
If you copy object A to object B in depth, the result is as follows:
The internal object of A is also copied to the internal object of B.
The above image should clearly express the difference between the shallow copy and the deep copy. Why is there a shortest copy and a deep copy? Yesterday, a friend gave me an answer using the C language. Unfortunately, I only learned the C language in college. Now I forgot about it, but which of my friends pointed outThe shortest copy and deep copy are generated by pointers. The condition is the transfer and return of values, which is essentially caused by the way data is stored in the memory..
Next I will explore the essence of deep copy. First I will talk about the truth in Java:In Java, all parameters are passed by reference..
References, references, or references. I have mentioned too many references.
I am tired of writing today. I will start from this reference in my next blog.
It is a great pleasure to study the programming language again. Especially after you have done some projects, you can look at the syntax knowledge of the language you are using again. You will feel quite open: you know that the knowledge is useful and difficult. More importantly, you know where you can review these technologies and what you can use them in the future. Some people may say that these are the basis, we have done a lot of projects, so why waste time? But if you have higher requirements for your technology, you want to make progress rather than cope with your work, it is my idea to learn something that will not quickly lead to bottlenecks for your growth. People who keep learning and working will always get more than those who are slack.