The second week 2.10-2.16
Day6
cycle
-
concept
- By a condition, the implementation of a logic code repetition
-
Implementation process
-
Program starts
-
Cycling conditions
-
true
- Cycle operation
-
false
- Cycle ends, execute subsequent code
-
-
-
-
The composition of the cycle
Requirements: 100 times print "helloworld"
package demo;
{class TestWhile public
public static void main (String [] args) {
int I =. 1; // initial portion; variable used to determine a
while (i <= 100) { // cycle conditions, decide whether to continue the cycle based
System .out.println ( "HelloWorld"); // cycle operation; a single code or logic execution task
i ++; // iteration portion; condition change control loop increment
}]
}
-
While Loop
-
grammar
while (Boolean expression) {
// logic code (cyclic operation)
}
-
Implementation process
- Boolean expressions first determination result is true, the code execution logic
- After this is finished, the determination is performed again, the result is still performed again subsequent code is true
- Results until the Boolean expression is false, will exit the loop structure, subsequent code execution
-
Note: The cycle has four parts
-
Features: The first condition that is the entrance, the first judge, and then perform for the number of cycles a clear case
public calss TestWhile{
public static void main(String[] args){I = 1000 Iint;
the while (I <= 1000) {/ determination is not satisfied for the first time, this time will not be executed (execution count: 0-n times)
System.out.println ( "the helloWorld");
}
the System.out .println ( "end of program");
}}
-
-
do while loop
-
grammar
do {
logic code (generation operating cycle)} While (Boolean expression);
-
Implementation process
- After the first time through the loop operation, and then determine the Boolean expression
- If the result is true, the operation execution cycle
- If the result is false, it will exit the loop structure, subsequent code execution
-
Features: no entry conditions for the first time, execute, and then judge
-
Scenario: suitable for cycles unclear circumstances
-
-
for loop
-
grammar
for (initial portion; cycling conditions; iteration section) {
// cycle operation
} -
Implementation process
- For the first time to perform the initial part (only once)
- Boolean expressions determination result is true, the code execution logic
- After this is finished, performs an iterative portion determination again, the result is still true, the logic code is executed again
- Results until the Boolean expression is false, it will exit the loop structure
-
Features: The first condition that is the entrance, the first judge, and then perform for the number of cycles a clear case
-
4 subtopics
-
-
Flow Control Keyword
-
break: termination, Switch out, the cyclic structure
public calss TestBreak{
public static void main(String[] args){for (int I =. 1; I <= 10; I ++) {
IF (I ==. 5) {
BREAK;
}
System.out.println ( "current loop number:" + I);
}
System.out.println ( " loop end ");
}
} -
continue: the end of this, to enter the next cycle.
{class TestContinue public
public static void main (String args) {
for (int I =. 1; I <= 10; I ++) {
IF (I ==. 5) {
Continue;
}
System.out.println ( "loop end") ;
}
System.out.println ( "loop end");
}}
-
-
NOTE: reference type can not be defined within the loop body, the basic data types may be defined inside the loop
-
Nested loop
-
Concept: In the intact loop structure, the nested structure of another complete cycle
-
example
public class TestNesFor {
public static void main (String [] args) {
for (int I =. 1; I <=. 3; I ++) {// outer control the number of rows
for (int j = 1; j <= 5; j ++ ) {// inner control columns
System.out.println ( "*");
}
System.out.println ();
}
}
}}
-
Day7
break and continue the training program
Breakpoints
-
Make a point to the left of the program line, debug
-
Right-watch
- F 8 run to the next breakpoint
-
function
-
The concept of functions
- Piece of code to accomplish a specific function can be used repeatedly
-
Defined functions: syntax
public static void function name () {// follow the naming identifier
// function body function code
}-
Defined position: the definition of a function within the class, the main () function in parallel
// location. 1
public class TestDefinitionFunction {
// position 2
public static void main (String [] args) {
// Position 3}
// Position 4
}
// position 5The correct position: position 2, position 4
-
Define a new function
{class TestFunction public
public static void main (String [] args) {
System.out.println ( "Moonlight");
System.out.println ( "suspected ground frost");
System.out.println ( " I raise my eyes to the moon ");
System.out.println (" bow their heads and think of home ");}
// Define: Print delimiter 10 functions
public static void printSign () {
for (int I =. 1; I <= 10; the I ++) {
System.out.println ( "-");
}
the System.out. the println ();
}}
-
operation result:
Moonlight, suspected ground frost, I raise my eyes to the moon, looking down and think of home
-
Note: The definition of the function did not change the results
-
-
-
Experience: a set of codes would need to be reused in a plurality of positions, defined within the function
-
Composition function
-
Call functions
- In the position we need to perform the function code, called by the function name
- Note: When you call the function, the function will give priority to the implementation of internal code, after the return to the calling of the function, proceed down
-
Benefits function
- Facilitate interaction between function and caller data is needed; the caller must provide the necessary data to complete the function of the corresponding function
- When you call the function, the incoming data is called "parameter"
Day8
Function Arguments
-
Function parameters
-
Parameter
-
Definition syntax
public static void function name (formal parameter) {
// function body
}- Experience: "parameter" is equivalent to the local variable declaration
-
-
Arguments
-
Call Syntax
-
Function name (actual parameters);
- Experience: " 'argument is equivalent to the local variable
-
-
-
-
A single parameter
{class TestFunction public
public static void main (String [] args) {
System.out.println ( "Moonlight ');
printSign ();}
public static void printSign(int count){
for (int i =1;i<count;i++){System.out.println("-");
}
System.out.println();
}
}// actual parameters: 10 calling a function with parameters, you must pass the actual parameters for the formal parameter assignment
// formal parameters: int count
when the function is executed, the cycle count times -
Multiple parameters
{class TestFunction public
public static void main (String [] args) {
System.out.println ( "Moonlight ');
printSign (10,' # ');
System.out.println (" suspected ground frost " );
System.out.println ( "lift eyes to the moon")
System.out.println ( "bow think of home"
}
// defined above; printing
}@ Arguments: 10, #
the call parameters, arguments passed sequentially, the type, number, sequence, must form the corresponding parameters of the@ Parameter: int count, char sign when the function is executed when the print count times sign
-
How to define parameters: Depending on business needs to set parameters of the function
Return Value and Return Type
-
Definition syntax
public static return type function name (parameter list form) {
// main function
return value; // Return value}
-
Call Syntax
Variable name = function ();
variable name = function (); -
1, a predetermined particular type of return value (base, reference, void) 2, 3, variable type consistent with the type of the return value variable type 4 and the type of the return value according to the requirements consistent returns a result (value)
-
example
{class TestResultValue public
public static void main (String [] args) {
int the Add Result = (5,6); // Return value received: calling the function expression, which represents the result returned
System.out.println ( result);}
Public static int the Add (A int, int B) {// Return Value Type: defining, i.e., the return type of the result agreedreturn a + b; // Return Value: returns the value to match the type of concrete results. DETAILED return value is added after the keyword
}
] -
The return keyword
-
A function can have only one return value
-
When a return is worth branching structure function exists, we must ensure that each branch has the correct return value
-
return of two ways:
-
Application type having a return value of the function: return value; // indicates the end of the current function, and with the return value of the function returns to the calling
-
Application of the function does not return type (void) in: return; // indicates the end of the current function, the function returns directly to the caller
Examples:
public static void Show () {for (int I =. 1; I <= 100; I ++) {
IF (I == 50) {
return; // this function to return directly End}
}
}
-
-
Note: a class can be defined in a parallel relationship between a plurality of functions belonging to the function, can be nested within
-
Experience: a function of only one thing
-
benefit:
- Kang Yu reduce code
- Improve the reuse rate
- Improve readability
- Improve maintenance
- Convenient division of labor
-
Day9
Multi-level calls
public class TestNestInvoke{
public static void main(String[] args){
m1();
}
public static void m1(){
System.out.println(“m1() -start”);
m2();
System.out.println(“m1()-end”);
}
public static void m2(){
System.out.println(“m2() --start”);
System.out.println(“m2() -end”);
}
}
-
operation result:
Operating results:
M1 () -start
M2 () -start
M2 () -start
M1 () -start -
Internal code execution priority function, after the return to the calling, continues down
Infinite recursion
public class TestRecursionInvoke{
public static void main(String[] args ){
m1();
}
public static void m1(){
System.out.println("m1() -start’);
m1();
System.out,println(“m1() -end”);
// When a function calls itself, without the proper export conditions, the resulting infinite recursion
}
}
Note: exception in thread "main" java.lang.StackOverFlowError memory overflow
The concept of recursion: the actual development, recursion can solve specific problems with a regular engine oil
Recursive scenarios: 1, when it is necessary to solve the problem you can open several small problem, the solution of the problem the same size 2, a fixed rule, a function, calls itself
How to use recursion properly: Set up effective export conditions, to avoid infinite recursion
Examples cycle factorial
Calculate the factorial of 55! = 5 . 4 . 3 2 . 1
public class TestFactorial{
public static void main(String[] args){
System.out.println(factorial(5));
}
public static int factorial(int n){
int sum =1;
for(int i =2;i<=n;i++){
sum*=i;
// Loop simple factorial calculation, by multiplying each value sequentially
}
return SUM;
}
}
Note: All problems can be solved recursively, the cycle can be resolved. When solving complex problems, recursive implementation simpler
Day10
Array
-
concept
- A set of contiguous storage space, storing a plurality of the same data type values
-
Features:
- The same type
- Fixed length
-
Creating arrays
public class TestCreateArray{
public static void main(String [] args){
int [] a = new int [5]; // allocate a continuous length of the space 5
}
}
// store 5 values of type int
-
First statement, redistribution space
- Data Type [] array name;
- = New array name Data type [length]
-
Declare and allocate space:
- Array type [] = new Array Data Type Name
-
Statement and assignment (Traditional)
- Array type [] = new Array Name Data Type [] {value 1, value 2, value3 ,,,,,,,};
-
And the assignment statement (simplified)
- Data Type [] array name = {value1, value2, value3} // initialize display, note: not wrap
-
-
Using an array of
public class TestCreateArray{
public static void main(String[] args){
int [] a = new int [ 5]; // create an array
a [0] = 5; // sequentially assigned
A [. 1] =. 3;
A [2] =. 4;
A [. 3] =. 7;
A [. 4 ] = 10;
System.out.println (A [0]); // turn values
System.out.println (A [. 1]);
System.out.println (A [2]);
System.out.println (A [. 3]);
System.out.println (A [. 4]);}
}- Each data array is referred to as "array elements'
- Assignment or value of each element of the operation is called "access elements"
- Access elements need to use "index" (from zero, the +1 order, automatically generated)
- Syntax visit: array name [index]; // save e.g. a [0] = 10; take: a [0];
-
Array indices
- Effective subscript range: 0 -1 array length
- Access invalid index will lead to cross-border array subscript
-
Iterate
public class TestVisitArray{
public static void main(String[] args){
int [] a = new int [5];
a[0]=5;
a[1]=3;
a[2]=4;
a[3]=7;
a[4]=10;
for( int i = 0 ; i<a.length;i++){
System.out.println(a[i]);
}
}}
- Traverse: from start to finish, one by one for each element of the array access
- .Length dynamically obtain the array name array length
- Using a loop variable "i" acts as a subscript, each element of the array can be accessed individually
-
The default value of the array
public class TestDefaultValue{
public static void main(String[] args){int []a = new int[5];
for (int I = 0; I <. 5; I ++) {
System.out.println (A [I]);
// in the absence of assignment of array elements, still can be accessed correctly.
}
}}
- The default value of the array: integer 0; 0.0 decimal character \ u00000 other Boolean false null;
-
Expansion Array
-
When you create an array, you must display the specified length, and length can not be changed after creation
-
Expansion of ideas:
- Create a new array larger than the original array
- In turn copied to the new array elements in the original array
-
-
Copy the way
- Assigned to the new array cycle one by one all the elements of the original array
- System.arraycopy (original array, the original start of the array, a new array, a new array starting length)
- java.util.Arrays.copyOf (original array, new length); // returns the original value with the new array
-
The replacement address
- As one type of array reference, which is the address stored in the variable array
- After the completion of copying elements, the need to address new array, assigned to replace the original variable
-
Array type parameters
public class TestArrayParameter{
public static void main(String[] args ){
int [] nums ={111,222,333,444,555};
printArray(nums);}
public static void printArray(int [] oneArray){for(int i= 0;i<oneArray.length;i++){
System.out.println(oneArray[i]);}
}
}- When the method is called, will the nums address assigned to oneArray, this time both point to the same array
-
Vararg
public class TestArrayParameter{
public static void main(String[] args ){
}
public static void printArray(int … oneArray){
for (int i =0;i<oneArray.length;i++){System.out.println(oneArray[i]);
}}
}
- Concept: a plurality of the same type may be received arguments, the number is not limited to using the same as the array
- Syntax: ,,,,, data type // parameter name must be defined at the end of the parameter list, and only one
-
Sorting an array
- Bubble sort: comparing the size of two adjacent values, interchanged
- Selection sort: fixed value and other values are sequentially comparing the size, position swap
- JDK Sort: java.util.Arrays.sort (array name); // JDK provides (in ascending order)
-
Two-dimensional array
- The concept: a bunch of one-dimensional array array, the array elements or array
- When looking for a cell in excle, two lower standard, on behalf of n-row, m for a multi-column two-dimensional array corresponding to the plurality of columns form rows
- Finding grid two-dimensional array "B3" unit X table syntax for x [3] [B] the front row index, after the column index
-
Two-dimensional array assignment
public class Test2DArray{
public static void main(String[] args){
int [] [] array = new int [3][5]; array[0][0] =10; array[0][3]=20; array[1][1]=30; array[1][2]=40; array[2][4] =50;
}
}
- Using double subscripting element two-dimensional array, a first index representative of: the row number (high dimensional subscript), the second subscript Representative: column number (low dimensional subscript)
-
Two-dimensional array of memory allocation
erwei
- High-dimensional array of every element, contains the address of low-dimensional array. Access the array [0] is equivalent to the access 0x0000A111
-
Two-dimensional array of access
public class Test2DArray{
public static void main(Sring[] args{
int [] [] array = new int [3][5];
array[0][0]= 10;
array[0][3]=20;
array[1][1]= 30;
array[1][2]=40;
array[2][4] =50;
for(int i = 0; i<array.length;i++){
for(int j =0;j<array[i].length;j++){System.out.println();
}
System.out.prinltn();
}}
}
- Access low dimensional length array [0] .length first length of the low-dimensional array
- Access to low-dimensional array elements; array [0] [0] the first element of the first low-dimensional array