Review:
1. Looping: Repeatedly executing a code of the same or similar
2. Cyclic three elements:
1) Initialization of cyclic variables
2) Conditions of the loop (based on the cyclic variable)
3) changes in cyclic variables (toward the end of the cycle)
Loop variable: The amount of change in the loop
3. Loop structure:
1) While: first judgment after execution
2) Do...while: First execution after judgment
The 1th element is the same as the 3rd element
3) for: Highest application rate
4.break: Jump out of the loop
Continue: Skips the remaining statements in the loop body and goes to the next loop
int num=1;
while (num<5) {
num++;
}
SYSTEM.OUT.PRINTLN (num); 5
int num;
for (num=1;num<5;num++) {
}
SYSTEM.OUT.PRINTLN (num); 5
Attitude
Don't finish your homework in order to finish your homework
Learn
I'm going to have to knock it all out on my textbook code.
Notes:
1. Better application of the various cycle structures
1) while: "When ..." loop
2) Do...while: "Until ..." loop
The 1th element is preferred with the 3rd element
3) for: Fixed number of cycles----highest application rate
2. Nested loops:
1) loop in the loop of the sleeve
2) Multi-row multi-column use, outer loop control row, Inner loop control column
3) Execution Process: Outer loop to walk once, memory cycle go all times
4) can use a layer without two layers, can use two layers without three layers, the less layer the better
If the three-layer loop is exceeded, the design has a problem
5) Break can only jump out of a layer of loops
3. program = algorithm + data structure
Algorithm: Process/step for problem Solving (order, branch, Loop)
Data structure: Save the information according to a specific structure--how many are stored
Designing a reasonable data structure can lead to good algorithms
4. Arrays:
1) Collection of elements of the same data type
2) An array is a data type (reference type)
3) Definition of the array:
int[] arr = new INT[4];
4) Initialization of arrays: assigning values to elements in an array
int[] arr = new INT[4]; 0,0,0,0
Int[] arr = {1,4,5,7}; 1,4,5,7
int[] arr = new int[]{1,4,5,7}; 1,4,5,7
Int[] arr;
arr = {1,4,5,7}; Compile error, this method can only declare simultaneous initialization
arr = new int[]{1,4,5,7}; That's right
5) Array Access: Use subscript/Index to access elements in the array
Subscript starting from 0, Max to. length-1
int[] arr = new INT[4];
System.out.println (arr.length); 4
ARR[0] = 100; Assigns a value of 100 to the 1th element in arr
ARR[1] = 200;
ARR[2] = 300; Assigns a value of 300 to the 3rd element in arr
ARR[3] = 400;
ARR[4] = 500; Array subscript out of bounds exception
System.out.println (Arr[arr.length-1]); Output last Element
6) Array Traversal:
int[] arr = new INT[10];
for (int i=0;i<arr.length;i++) {//Traverse arr Array
Arr[i] = 100; Assign a value of 100 to each element in arr
}
for (int i=0;i<arr.length;i++) {
System.out.println (Arr[i]);
}
for (int i=arr.length-1;i>=0;i--) {
System.out.println (Arr[i]);
}
7) Copying of arrays:
System.arraycopy (a,1,a1,0,4);
int[] A1 = arrays.copyof (a,6);
A = arrays.copyof (a,a.length+1);
8) sort the array:
Arrays.sort (arr); Ascending row
Task:
1. Bubble sort algorithm for 4 numbers sort--int[] arr = {45,23,87,1};
Write execution process----to the program with a number of
2. The classic case is very skilled to write
3. After-school assignments
4. Daily Practice
-------improve logical thinking during dahne (think, practice)
Listen to me, I can understand.
I can't write it-------normal.
1) Logical thinking ability is not enough
2) write too little----write a few more times, go to the program with the number
Bubbling principle:
1) 4 counts 3 rounds
2) Each round is starting from a 1th element
3) Each time is compared to its next element
4) The number that comes out is not playing with it.
Arr.length-1-i
I=0 than 3 times
I=1 than 2 times
I=2 than 1 times
Int[] arr = {45,23,87,1};
for (int i=0;i<arr.length-1;i++) {//Control wheel number
for (int j=0;j<arr.length-1-i;j++) {//control the number of comparisons per round
if (Arr[j]>arr[j+1]) {
int t = arr[j];
ARR[J] = arr[j+1];
Arr[j+1] = t;
}
}
}
First round:
45 and 23, 23,45,87,1.
45 and 87 ratio, no change, 23,45,87,1
87 and 1, 23,45,1,87-----------87.
Second round:
23 and 45 ratio, no change, 23,45,1,87
45 and 1, 23,1,45,87------------45.
Third round:
23 and 1, 1,23,45,87------------23.
1) If the number of elements is small, what sort of method is used without affecting
2) The number of elements, the ranking algorithm has the advantages and disadvantages of the
Less exchange times more efficiency
3) different data using different sorting algorithm, the final efficiency is also different
Long a = System.currenttimemillins (); 1010
Bubbling algorithm
Long B = system.currenttimemillins (); 1012
System.out.println (B-A); The number of milliseconds that Arrays.sort (arr) spends
100,000-----The fastest bubbles
100,000-----Fast and fastest
100,000-----Insert the fastest
Arrays.sort (arr); Ascending order (from small to large)
Int[] arr = {1,5,76,45};
Algorithm to find the maximum number:
1. Assume that the 1th element is the maximum value
int max = arr[0];
2. Traverse the remaining elements, comparing each element to Max,
If it is greater than max, change the value of Max to a larger number
for (int i=1;i<arr.length;i++) {
if (Arr[i]>max) {
max = Arr[i];
}
}
Int[] A = {10,20,30,40,50};
int[] A1 = new INT[7]; 0,0,0,0,0,0,0
for (int i=0;i<a.length;i++) {
A1[i] = A[i];
}
Max=1
I=1 max=5
i=2 max=76
I=3
int[] arr = new INT[10];
System.out.println (arr[9]);
System.out.println (Arr[8]);
System.out.println (Arr[7]);
System.out.println (Arr[6]);
System.out.println (Arr[5]);
System.out.println (Arr[4]);
System.out.println (Arr[3]);
System.out.println (arr[2]);
System.out.println (arr[1]);
System.out.println (Arr[0]);
int[] arr = new INT[10];
for (int i=0;i<arr.length;i++) {
Arr[i] = 100;
}
I=0 arr[0]=100
I=1 arr[1]=100
i=2 arr[2]=100
...
I=9 arr[9]=100
ARR[0] = 100;
ARR[1] = 100;
ARR[2] = 100;
ARR[3] = 100;
ARR[4] = 100;
ARR[5] = 100;
ARR[6] = 100;
ARR[7] = 100;
ARR[8] = 100;
ARR[9] = 100;
int[] arr = new INT[4];
System.out.println (Arr[0]); 0
ARR[2] = 88; Assigns a value of 88 to the 3rd element
Compile error, check syntax
int[] arr = new INT[4];
int A;
int b;
int C;
int D;
b = 88;
int a = 5;
Declares an array of type int arr, containing 4 elements
Each element is of type int and the default value is 0
int[] arr = new INT[4]; 0,0,0,0
Declares a double array dou, containing 8 elements
Each element is a double type with a default value of 0.0
double[] Dou = new DOUBLE[8];
Declares a Boolean array bos, containing 20 elements
Each element is a Boolean and the default value is False
boolean[] Bos = new BOOLEAN[20];
Place books according to the category of books----the algorithm for finding books is simple
It's not easy to place a book in a different publisher----algorithm
In practice, it is necessary to jump out of all levels in a certain situation---demand is almost
for (int i=1;i<=10000;i++) {
for (int j=1;j<=10000;j++) {
for (int k=1;k<=10000;k++) {
System.out.println ("AAA");
Break
}
}
}
Software is to simulate real-life
I=1
J=1/2/3/.../19/20
i=2
J=1/2/3/.../19/20
I=3
J=1/2/3/.../19/20
day005--20150805