---the definition of an array---Organization stores a set of data
1. A collection of data of the same type--a container is essentially a container--the difference is a container that stores only one data--facing containers, not scattered data
eg. buy a plate of eggs--the egg tray is actually a container.
2. The array can start numbering from 0--so that each data can be easily accessed by subscript
3. An array is essentially an object, the array name is the address where the array object is stored, the elements of the array are equivalent to the member variables of the object--it is initialized by default
4. Array declaration and initialization format--
* Direct declaration and initialization (static initialization mode) int[] arr = {n/a};
* First create reinitialization int[] arr = new INT[3]; arr = {A-i};
* Choice of two ways of definition
Static initialization---requires a container and explicitly stores the elements
Dynamic initialization---requires that the container be ambiguous for the element to be stored
-----Data---Automatically associate the knowledge that containers are used to store-----design to the container-the computer is designed to deal with real-life data-how to store the different relationships between data---data---may choose different containers for storage
int--represents the type of an array element
arr--array name represents the entire array essence is the address of the numeric object
new--the keyword used to create the object
3--represents the length of the array (cannot be changed once determined)
---The JVM runs the program when the application to the memory partition---partition reason different region data processing way different
(In fact, many of the applications to the memory will also be divided)
1. Register--CPU related, do not pay too much attention
2. Local method area-related to the system that the JVM belongs to
3. Method area
4. Stack memory--store local variables (defined inside the method)
Local variables are loaded--the main function is loaded--the method loaded--the variable is created in the stack and assigned--once the scope of the variable is released, the space taken up by the variable is freed--and it is freed when the method finishes executing.
Stack memory update is fast
5. Heap memory--used to store all object entities (whatever is new)--the characteristics of the heap
1. Each entity has an initial address value-a reference to the object that is assigned to it
2. Each entity will have its own data that does not affect each other-and will be initialized by default
Integer-0 float-0.0f double-0 boolean-false char-' \u0000 '
\u represents the corresponding form of Unicode encoding--literal number sign, etc. in computer
3. Reference data type variable--references/points to an entity in heap memory, or does not point to any entity-null
4. The memory occupied by the object is not automatically released--java has a unique GC garbage collection mechanism
(There will be objects that will occupy memory in the heap--when it's time to recycle to see the specific garbage collection algorithm control)
C + + is the programmer's Manual garbage collection Garbage--otherwise there will be a memory overflow
5. Assigning values between reference variables-the result is executing the same object
As long as there is a reference variable pointing--the object is not garbage, it is definitely not recycled
---the memory partition when the array is defined---
Creating a reference to an object--creating an object--assigning an object's address to a reference to an object
---Small details of array manipulation---
1. Subscript range 0-length-1 (access out of range will subscript out-of-bounds exception) int[3]--arr[3] Compile legal, run out of bounds ArrayIndexOutOfBoundsException
2. Compile time is to check the syntax error, the operation will really occupy the memory space
3. An entity that is not pointed to cannot be manipulated--nopointerexception-null pointer exception appears
4. Print array name--essentially the object's first address symbol + number (hash value-find address by value) hash algorithm--call Windows hash algorithm--operating system control memory allocation
Symbol-Type of entity
5. Accessing elements in an array by subscript
---array-related operations---the most fundamental operation: the number of bytes and the number of the fetch (variables also)
Core idea: It is the diagonal mark to operate
1. Iterating over elements in an array--loop array length length--is the property of the array itself
2. Get the maximum value (two implementations)
Suppose the first position is the largest (comparison of the angle index) Arr[maxindex] vs. Arr[i]-Returns the maximum worthy of a corner mark
Suppose the value of the first position is maximum (value comparison) Max and arr[i] comparison-Returns the maximum value
3. Sort (bubbling, selection)--the basic action is to compare and exchange (Element)
Select Sort-thought: the first one for the benchmark and the next one to find the smallest element and the first interchange--to calculate the order once, after the final element is ordered in order of many times.
* * Select sort efficiency optimization--swap directly with two variables to record the smallest position within the loop comparison.
Performance optimization is meaningful for more elements
Sort count = number of elements-1; the number of times each comparison is decreasing, the position of one element is determined by each sort
bubble sort-comparison between adjacent elements-interchange, maximum or minimum for each sort
4. Sequential lookup (most common--for unordered arrays)-Inefficient
Binary lookup (for-data-ordered)-Narrow the range (low mid high set 3 subscript)
for binary find each time--arr[mid] is equal to the key value, The conditions for the loop judgment are the following two types of
--arr[mid] are equal to key
--low <= high (indicating that the element you are looking for is not in the array once the low >high is present)
Array once the length of the created array is fixed
java--inside the Util bag there is a Arrays class that defines many common ways to manipulate arrays
It's best to master the code for these common sort lookups--other languages don't necessarily provide the appropriate method
Arrays.binarysearch ()
Find a value that does not exist-(-min-1)--tell the nonexistent and insert position
to find the value that exists--the position of the returned subscript
-----The application of the array----
1. When to use an array---important
if the data has a corresponding relationship, If the corresponding party is an ordinal number, as a corner label--you can use the array
to store the data in an array, using the result as a corner label--access to the data---look up the table method (more common applications)
data--as long as the more you need to consider the storage of arrays centralized processing
Array--used as a temporary container using
2. Binary conversion (Basic data Type encapsulation class contains the method of digital conversion)
Decimal---binary &1 >>>1
Decimal---octal &7 >>>3
Decimal---Hexadecimal &15 >>>>4
Observe the above rules & variables 1 Right-shift variable 2 defines the method when the parameter is passed, the definition of a temporary array to find out the data stored in the table, print the final result. temporary array [+]
for the application of table-checking---There is a corresponding relationship and an ordered number
----a two-dimensional array---Array of arrays
1. Two-D array definition
* * int[][] arr= new int[3][2];
Arr.length = 3 Creates a two-dimensional array that contains 3 one-dimensional arrays, each of which contains 2 elements in a one-dimensional array
* * Define the length of a two-dimensional array, and then define the length of one-dimensional array separately
int[][] arr = new int[3][];
Arr[0] = new INT[2];
ARR[1] = new INT[3];
ARR[2] = new INT[4];
Static initialization
Int[][] arr = {{1,2},{3,4},{5,6}};
22-dimensional array name arr--remains the first address of a two-dimensional array
arr[0]--represents a one-dimensional array address labeled 0 in a two-dimensional array
arr[1]--represents a one-dimensional array address labeled 1 in a two-dimensional array
arr[2]--represents a one-dimensional array address labeled 2 in a two-dimensional array
ARR[I][J]---Print all elements in a two-dimensional array (nested loops)
3. Memory partitioning for two-d arrays
A two-dimensional array name points to an entity with 3 references, 3 references to 3 one-dimensional array with 2 elements, respectively
Entities have their own memory places in the heap
Remember that all output statements are printed as strings, and each time the JVM is started it will be re-
4. Print the length of the two-dimensional array arr.length
Prints the length of a one-dimensional array in a two-dimensional array arr[i].length
5. Traversing the length of a two-dimensional array--for loop nesting
6. Application scenarios for two-D arrays
Data is many, and to classify, the array is also stored---in order to appear multidimensional array
Multiple bottles-Multiple boxes-one box (more convenient to process data)
---Core is to master the principle of implementation + thinking-----is not the use of the API + framework use
Java Basics Summary--arrays