Array
Concept : A collection of elements of the same data type.
you can store basic data types, or you can store reference data types.
wording :
One, declare the array and allocate memory, and initialize it. A static array.
int [] A = new int [] {1,2,3,4,5];
second, the declaration of the same time, according to the specified length of memory allocation. Dynamic array.
Employee [] e = new Employee [s] (employee is a custom class ).
Third, the relatively brief wording.
int [] b = {1,2,3,4,5};
length :Length Property
When you create an array, you must specify its length, and once defined, it cannot be changed.
calling method:int [] B1 = new int [] {1,2,3,4,5,6,7};
System.out.println (b1.length);
the array has a length of 7, but the memory actually gives 8 locations. The other one stores "7".
Assignment Value :
In the C language, you cannot assign an array directly to another array;
This is allowed in Java, where two arrays refer to the same memory.
Visit :
Array name [ index ] For example: a[1];
System.out.println (a[1]);
Note: The index starts from 0 ;
The data type of the index is integer type;
Index maximum and array length are always 1.
Iteration :
First type:for Loop
for (int i=0; i<a.length; i++) {
System.out.println (A[i]);
}
The second type: Enhanced for loop (Enterprise development is more commonly used)
notation:for (the type of the array element is the temporary variable name: The name of the array ) {
SYSTEM.OUT.PRINTLN ( temporary variable name );
}
That
for (int x:a) {
SYSTEM.OUT.PRINTLN (x);
}
Operator
Four types of operators:
arithmetic operators; bitwise operators; comparison operators; logical operators
Note:1, except = = and! =,+ , other operators are only for basic data types;
2, = = and! = The reference type can also be calculated;
3, + can also be connected to the string operation.
Arithmetic operators:
+ ,- ,* ,/ ,% ,a>b? True: False
Bitwise operators:
First, the binary is converted into binary operation. Two main categories:
1, with (&) or (| ) Non-( ~ ) XOR ( ^ )
Example:int c = 5, D; 5:0000 0101; 12:0000 1100
D = c^12; XOR: The same is 0, the difference is 1
System.out.println (d); 0000 1001 Result is 9
D = C & 12; with: All is 1 result is 1, the rest is 0
System.out.println (d); 0000 0100 Result is 4
D = C | 12; or: As long as one is 1, the result is 1, the rest is 0
System.out.println (d); 0000 1101 Results for
D = ~c; non:+1 Take counter
System.out.println (d); -(5+1) = =-6
2. shift operators:<<,>>(signed right shift),>>>(unsigned Right shift) ;
shift left 1 is the equivalent of multiplying by 2, and the right shift by 1 is the equivalent of dividing by 2.
Example: int a=8,c; A original value:0000
c = a>>2; after move:0000 0010
System.out.println (c); C value is 2
Comparison operators:
type:==,<,>,<=,>= ,!=
except for = = and ! = , the rest can only compare basic data types.
= = and ! = can also compare reference types (compare virtual addresses).
Note: The return value of the comparison operator is true or false.
logical operators
application: An expression for connecting two boolean types
&& (double and), | | (double or), & (with), | (or), ^ (XOR), ! (non)
Note: && & The results are the same, for & for && false Right is no longer calculated, return directly false
|| with the | The result of the operation is the same, for | the right side is involved in the operation, regardless of the value on the left. || , as long as the left side is true the right side is no longer calculated and returned directly true .
&& | | can be understood as a physical short circuit. , multi-use in practice && and ||
Process Control
type: sequential process, branching process, cycle flow
Branching process
An If condition expression can be any type of logical expression.
If the expression value is true, the following statement is executed after the contents of the curly brace are executed.
If the expressed value is false, the following statement is executed directly.
Format:
if ( expression ) {
statement block;
}
else{
statement block;
}
Switch the value of an expression can only accept int,byte,char , short , enum, String , do not accept other types of values, do not allow duplicate Case value;
Once the switch encounters the first case match, the program jumps to the tab location and starts executing all of the program code in the sequence, regardless of the case Condition matches until a break statement is encountered .
Format:
Switch ( expression )
{
Case takes value 1: statement block 1; break; If there is no break, the following case statement willcontinue to execute .
...
Case value n: statement block N; break;
Default: statement block n+1
}
Cycle flow
while Statement
Format:
while ( conditional expression )
{
Executes the statement block;
}
do ... while Statement
Format:
Do
{
Executes the statement block;
} while ( conditional expression );
For Loop
Format:
For ( initialization expression; loop conditional expression; loop execution Statement block 1)
{
EXECUTE statement block 1; conditions established
}
EXECUTE statement block 2; conditions are not tenable
Break
Used to terminate execution of a block of statements. Used in the Loop statement body, you can forcibly exit the loop.
The break statement can appear in the while ,do... while , for , Switch the body of the statement.
Continue
Used in the Loop statement body to terminate a loop, skipping the loop that is not executed under the Continue statement in the loop body, and starting the next loop process.
The Continue statement can only appear in the Loop statement while ,do... while , for the.
A summary of arrays, operators, and process controls