How to use Java array correctly in instance parsing
1、 On the characteristics of arrays 1 In Java, whether you use arrays or collections, there are boundary checks. If the operation is out of bounds, you will get a runtimeException. 2. Arrays can only hold specific types. Arrays can hold native data types, collections cannot. Collections do not handle objects with specific types. They treat all objects as object types. The references of objects rather than the objects themselves are stored in collections. 3. Collection classes can only save references to objects. Arrays can be created to hold either native data types directly or references to objects. In the collection, wrapper classes, such as integer and double, can be used to save the values of native data types.
Example code:
4. Object array and native data type array are almost the same in use; The only difference is that the object array holds the reference, and the native data type array holds the value of the native data type.
II Correct use of arrays
If you need to store a large amount of data, for example, if you need to read 100 numbers, you need to define 100 variables. Obviously, it doesn't make much sense to write the code 100 times. How to solve this problem? The Java language provides the data structure of array. It is a container that can store elements of the same data type and 100 numbers into the array. At this time, the array is of great help~
1. Advantages of array
Is there any difference between saving data and not saving data? The biggest advantage of arrays is that they can automatically number the stored elements Note that the number starts at 0. It is convenient to operate these data.
For example, the student number can be used to find the corresponding student.
2. Array format
Format I:
Element type [] array name = new element type [number of elements or array length];
Example:
Format 2:
Element type [] array name = new element type [] {element, element,...};
Note: when allocating space to an array, you must specify the number of elements that the array can store to determine the size of the array. You cannot modify the size of an array after it is created. You can use the length property to get the size of the array.
3. Declare array variables
In order to use an array, you must declare the array in your program and specify the element type of the array
=Left half:
First, the left side specifies that the element type is int and the container uses an array. How to identify the array Then it is represented by a special symbol [] brackets. If you want to use an array, you need to give the array a name, so we name the array x here Then follow the equal sign.
Code embodiment:
int [] x
Note: int x [] is also a format for creating arrays. It is recommended to declare arrays in the form of int [] X.
4. Create an array
=Right half:
To use a new keyword It's called new. New is used to generate a container entity in memory. Data needs space to be stored. The space for storing a lot of data is opened up by the new operator, new int [3]; This 3 is the number of elements. The right part defines a real array in memory, which can store three elements.
New int [3] does two things. First, create an array with new int [3], and then assign the reference of this array to the array variable x.
int [] x=new int[3];
What type is x?
Any variable must have its own data type. Note that this x is not of type int. Int represents the type of element in the container. Then x is of array type.
An array is a separate data type. The data types are divided into two categories: basic data type and reference data type. The second is the reference data type. Now you have come to one of the three types of reference data. Is the array type [], and the brackets represent the array.
int[] arr = new int[5]; What happens in memory?
Memory any program needs to open up space in memory when running int[] arr = new int[5]; What does this program look like in memory? This involves the space opened up by the Java virtual machine when executing the program. How much space does Java open up and start? Continue to learn the memory structure of Java.
5. Array initialization
Method 1: do not use the operator new
Method 2: use the operator new
If the operator new is not used in array initialization. Note: the following wording is wrong.
At this time, when initializing the array, you must put the declaration, creation and initialization in one statement. Separating them will cause syntax errors.
Therefore, it can only be written as follows:
6. Array traversal
There is an attribute in the array that can get the number of elements in the array, that is, the length of the array Array name length
7. Common exceptions of array
An array subscript is out of bounds. Exception:, note: the array subscript starts from 0.
Second null pointer exception:
Array:
When to use arrays: when there are many elements, in order to facilitate the operation of these arrays, temporary storage will be carried out first, and the container used is arrays.
characteristic:
The array length is fixed.
8. Common operations of array
1: Case:
An array fetches the maximum value
/*Define a function to get the maximum value:
1. Determination result: return value type int
2. Unknown content: if the maximum value of which array to obtain is not determined, the array is not determined
Idea:
1. Define a variable to record the larger elements of the array.
2. Traverse the entire array and compare each element of the array with the variable.
3. When a variable encounters an element larger than it, the variable records the value of the element. When the loop ends, the maximum value is generated
2: Direct sort
Case 2: use direct sorting to sort the array:
/*
Select sort.
Compare the element marked with one corner with other elements.
At the first end of the inner cycle, the maximum value appears on the head corner mark position.
3: Bubble sorting
4: Half search (dichotomy)
5: Array flip
11. Two dimensional array
Use of arrays
Detailed explanation of Java array
Traversal: tostring() returns the elements of the array as a string
Sort: sort() sorts the arrays in ascending order
Find: binarysearch() finds the specified element in the specified array and returns the index of the element. If it is not found, it returns (- insertion point - 1). Note: when using the find function, the array must be sorted first.
2D array:
Smoking:
No money to buy a variable
With a little money, there are 20 variables in a one-dimensional array
Very rich, a 10 Pack (two-dimensional array) two-dimensional array
Two dimensional array: the essence is that the storage is a one-dimensional array.
Array definition:
Array type [] [] array name = new array type [number of one-dimensional arrays] [number of elements in each one-dimensional array];
Detailed explanation of Java array
Question: why A. length = 3, a [0] length = 4?
Detailed explanation of Java array
Initialization of array:
Static initialization:
int [][] a = new int[][]{ {12,34,45,89},{34,56,78,10},{1,6,4} };
Dynamic initialization:
Detailed explanation of Java array
Common operations for 2D arrays:
1. Traverse two-dimensional array
2. Sum two-dimensional arrays
The above is all about Java arrays. As you can see, Java arrays are a very powerful data structure.