Why is Java? It seems to be related to Oak and Java. Why? Speaking of this reminds me of a song: Don't ask.
The simple text in the previous article gives you some starting points. So today we are looking at some slightly more complex Java items.
[Java array]
In Java, arrays similar to C/C ++ are also provided. In Java, arrays emphasize the concept of set, which has the characteristics of a set. It is very easy to declare arrays in Java. The syntax format is as follows:
As shown in:
Datatype array_name [];
Or
Datatype [] array_name;
The declared array only defines the array name and does not construct an array object. To construct an array object, we need to use the new operator to construct it, as shown below:
Datatype [] arrayname;
Array_name = new datatype [array_length];
Array reference and C, index by subscript (INDEX); its index_min = 0, index_max = array_length-1;
Next we will test the array:
/* This program test the control flow chart */import Java. util. *; public class CFC {public static void main (string [] ARGs) {// array test int [] array_int; array_int = new int [10]; required getdata = new partition (system. in); For (INT I = 0; I <array_int.length; I ++) {system. out. println ("Enter the" + (I + 1) + "number:" + ~ 0x7fff + "<=" + "Number" + "<=" + 0x7fff); array_int [I] = getdata. nextint () ;}for (INT I = 0; I <array_int.length; ++ I) {system. out. println ("array_int [" + I + "]:" + array_int [I]) ;}}
The aboveProgramDefines an array_int array, input 10 values through the values object, and print the array. Run the following command:
From this we can see that we can get the length of the array through the Length attribute of the array, which is different from C/C ++. In C, the programmer needs to control the length by himself, time Constant
It is easy to cause confusion.
Sometimes the variable-length array is required, and the variable-length array is not supported in c89. It seems that c99 provides limited support. In Java, the variable-length array is also supported through the class library, we will discuss it later.
after understanding the definition and use of arrays, we will perform a slightly challenging task and sort the input data. Below we will slightly change the above procedure, to achieve this function:
/* This program test the control flow chart */import Java. util. *; public class CFC {public static void main (string [] ARGs) {// array test int [] array_int; array_int = new int [10]; required getdata = new partition (system. in); // Prompt user input data: system. out. println ("Enter 10 numbers:" + ~ 0x7fff + "<=" + "Number" + "<=" + 0x7fff); For (INT I = 0; I <array_int.length; I ++) {array_int [I] = getdata. nextint () ;}// print the array for (INT I = 0; I <array_int.length; ++ I) {system. out. println ("array_int [" + I + "]:" + array_int [I]);} system. out. print ("\ n"); // sorts the array. The simplest method is int temp = 0; For (INT I = 0; I <array_int.length; + + I) for (Int J = array_int.length-1; j> I; j --) {If (array_int [J] <array_int [J-1]) {temp = array_int [J-1]; array_int [J-1] = array_int [J]; array_int [J] = temp;} // print the array for (INT I = 0; I <array_int.length; ++ I) {system. out. println ("array_int [" + I + "]:" + array_int [I]) ;}}
The aboveCodeThe running result is:
There are several special points for Array Processing in Java:
1. array initialization. New Initialization is not required.
Int [] int_array = {1, 2, 3, 4}; // defines an int-type array variable, int_array, which has Index = 0, 1, 2, 3
2. Anonymous Array
Int [] int_array = new int [] {1, 2, 3, 4, 5}; // The Int [] {1, 2, 3, 4, 5} following it is an anonymous array.
Here we use an anonymous array to initialize the array. Note that the initialization here is the same as the initialization in C.
3. Copy an array
Java provides an array replication mechanism to replicate Arrays Using the copyof () method of the arrays class.
You can also use the system. arraycopy method to intercept and copy arrays. The syntax format is: system. arraycopy (from, index_start, to, copy_length );
These two methods are mainly used to increase the length of the array. We will not give an example for specific applications.
[Java if statement]
If statements are frequently used, so here we will introduce the if statements. If you have been in contact with C/C ++, therefore, the use of IF Statements in Java will be very simple.
The IF statement is the same as that in C/C ++. The syntax format is as follows:
Style 1:
If (boolen condition) Statement; other statement; // if the judgment condition in the IF statement arc is true, execute statement; otherwise, execute other statement.
Style 2:
If (Boolean condition)
If_statement;
Else
Else_statement;
Other_statement;
If Boolean condition is true, execute the if_statement Statement (which can be a statement block) and then execute other_statement. If Boolean condition is false
Execute else_statmetn (which can be statement blocks) and then execute other_statement.
Style 3: Nested if statement
The nested if statement follows the preceding two rules, and the else part matches the latest if part. As follows:
If (Boolean condition 1)
If (Boolean condtion 2)
Statement;
Else
Statement;
As shown above, the else clause matches the if clause of Boolean condition 2 to form an internal nested if clause. To make the program code readable, it is recommended that
Add {} to construct the statement block to better differentiate if and else matching conditions, as shown below:
If (Boolean condition 1)
{
If (Boolean condtion 2)
Statement;
Else
Statement;
} // End of Boolean condition 1 if
[Java while loop statement]
The while loop statement in Java is similar to the while statement in C/C ++.
Java also provides a do... while loop, do... while loop body, which must be executed at least once.
Note: Some books call the while loop a "when" loop, that is, when the condition is not true, the loop ends .. a while loop is called a "" loop, and ends the loop when the condition is not met.
[Java for loop statement]
A Java loop for loop is usually used to determine the number of cycles (of course, you can also use a loop with an uncertain number of cycles). The structure of a For Loop in Java is similar to that in C/C ++, I will not describe it here;
It is worth noting that:
You can define the available variables in the for statement in the for loop statement. These variables are only visible in the for statement. When the variables defined in the for statement are the same as those defined outside the for statement, then
Symbol redefinition error, as shown below:
/* This program test the control flow chart */import Java. util. *; public class CFC {public static void main (string [] ARGs) {// array test int [] array_int; array_int = new int [10]; // defines the I variable, check whether the I variable int I; I = 100 in the for statement is affected; Response getdata = new response (system. in); // Prompt user input data: system. out. println ("Enter 10 numbers:" + ~ 0x7fff + "<=" + "Number" + "<=" + 0x7fff); For (INT I = 0; I <array_int.length; I ++) {array_int [I] = getdata. nextint ();}
This is different from the Mechanism in C/C ++ where small-scope variables block large-scope variables.
In the for statement and while statement, you must note that each statement in the for loop body has the opportunity to be executed. Otherwise, an error is reported, this is also true in many C/C ++ programs.
It often appears.
1 public class break 2 {3 Public static void main (string ARGs []) 4 {5 Int int_var = 10; 6 7 for (; int_var> 1;) 8 {9 break; 10 system. out. println ("Can you see this statement !!! "); 11} 12 13 system. Out. println (" break statement Hava interupt the if statemtn "); 14} 15}
The above code compilation result is as follows:
[Java Enumeration type]
Why do we mention the enumeration type here? As we will talk about the switch statement next, we can see that the switch statement has a "natural" relationship with the enumeration type.
Java also provides enumeration types, which are very convenient when dealing with some problems. For example: month, week, mall fruit type. Defining enumeration types in Java is also very easy;
The syntax format is as follows:
Enum week {Mon, Thu, Wend, ths, Fri, SAT, Sun };
In this way, a week Enumeration type is defined (here we do not separately declare and define them, and we do not limit the difference between declarations and definitions in C/C ++ ), we can use it to define variables:
Week vication;
Vication = week. Mon; // note that when assigning an initial value, you must specify the enumeration type. However, in special cases, you do not need to specify the enumeration type. We will not talk about it here. Below is my
A link to the search by Du Niang: The http://www.cnblogs.com/Fskjb/archive/2009/08/03/1537917.html contains a detailed introduction to enumeration types.
Key points:
The enumerated type variable can be assigned null, indicating that the enumerated type variable is not initialized;Note that null does not indicate that the enumerated variable does not point to the specified memory. After defining the enumerated variable, the memory has been allocated.
It is different from null in C.
[Java switch statement]
The swith statement is applicable when you know how to select a project set. For example, if we buy clothes at the mall, there are different brands in it. We define the type of a clothing category through Enum, and then I can
Determine the price, discount, and other information based on the user's choice.
The switch statement in Java is the same as that in C/C ++. For details, refer to the above link.
Note that if you use the Enum and swith statements in Java, you do not need to specify the enumerated type information in the case clause section of the swith statement, for example:
Week workday = week. Sun;
Switch (WorkDay)
{
Case Mon:
Statement;
Break;
Case week. Thu:
Statement;
Break;
.......
}
As you can see, in this case clause, you do not need to specify the enumeration type name week. You can also specify it, as shown in the above example.
Key points:
1. The break statement must be added after each case clause. Of course, this is not necessary. We can select several case clauses to execute a statement (statement block ).
2. The default clause can be used in the switch statement, which is the same as the C language. Not to mention.
[Java break and continue statements]
A break statement is used to interrupt a normal control flow. It is generally used to jump out of loops and switch statements.
For example:
/* This program test break statement */public class break {public static void main (string ARGs []) {int int_var = 10; // can execute forever ?? For (; int_var> 1;) {system. Out. println ("Can you see this statement how many times ??? "); Break;} system. Out. println (" break statement Hava interupt the if statement ");}}
The execution result is as follows:
How many times can the following program be executed?
1/* 2 this program test break Statement 3 */4 5 public class break 6 {7 public static void main (string ARGs []) 8 {9 int int_var = 4; 10 12 for (; int_var> 1; int_var --) 13 for (INT I = 0; I <5; I ++) 14 {15 system. out. println ("How many times can you see this statement ??? "); 16 break; 17} 18 19 system. Out. println (" break statement Hava interupt the if statement !!! "); 20} 21}
What do you think of this puzzle? I will not explain the results here. If you are interested, try it by yourself. Of course, before the test, you can say how many lines will the for loop weight statement output ??.
Sometimes, in a nested loop, In order to jump out of the entire large loop, it is to jump out and execute the statements following the loop. What should we do? Another mechanism is provided in Java: Break label. We
The above code changes a little bit to see what the results will look like:
/* This program test break statement */public class break {public static void main (string ARGs []) {int int_var = 3; // test the break label mechanism: break_label: for (; int_var> 1; int_var --) for (INT I = 0; I <5; I ++) {system. out. println ("How many times can you see this statement ??? "); Break break_label;} system. Out. println (" break statement Hava interupt the if statement !!! ");}}
What is the result after this code is executed? Let's get it done:
No. The for loop body statement is executed only once.
Key points:
The break statement jumps out of the loop contained in the loop body, while the break label statement can jump out of the cycle of the break label mark. That is, the break matches the loop body with the "nearest", while the break label matches
The cyclic match marked by the break label.
Like the C language, the continue statement breaks the execution process of the loop body and does not jump out of the loop. We can use the following program to compare the difference between break and continue:
1/* 2 this program test break Statement 3 */4 5 public class break 6 {7 public static void main (string ARGs []) 8 {9 int int_var = 4; 10 11 12 for (; int_var> 1; int_var --) 13 for (INT I = 0; I <5; I ++) 14 {15 if (I = 1) 16 continue; 17 18 system. out. println ("How many times can you see this statement ??? "); 19 20} 21 22 23 system. Out. println (" break statement Hava interupt the if statement !!! "); 24} 25}
The above program execution result:
Let's look at the following code:
As we can see, continue cannot jump out of the loop body, while break can jump out of the loop body.
In this summary, we will give a rough description of the control process of Java's structured programming, which will be further discussed later.
I feel that writing these things is a summary of my own learning process. I have been familiar with C/C ++ before, so sometimes the content I write will be a bit interspersed. However, such insertion may not be appropriate.
There are no people who have basic computer programming skills.