Traverse
Traversal is the way that each element of this array is shown to iterate by defining the size of the array and then using the For loop to complete the array, for example
Double[] score = new Double[5]; Scanner input = new Scanner (system.in); for (int i = 0; i < score.length; i++) { System.out.println ("Please enter" + (i + 1) + "Student's score:"); Score[i] = input.nextdouble (); } for (int i = 0; i < 5; i++) { System.out.println (score[i]); }
This is a double array with for to go through each element and the end condition is that the last array of the array score.length can traverse the array.
Sum
Summing is the addition of a summation counter on the basis of a traverse to define a variable for the outside of the for and the initial value of the variable is 0.
So that we can achieve the sum of the results is correct
The sum formula is written in the for loop because the sum is also the process of adding to the loop.
For example
Double sum = 0; int i; for (i = 0; i < score.length; i++) { sum = sum + score[i]; } System.out.println (score.length + "Student's score is" + sum);
This is the process of summation, and the other way to do this is to change the symbol.
Extremum
The goal of the extremum is to find an extreme value
The method is to assume that one of the data in the array is the largest and then use the other data to traverse him. If the new data value is larger than the value of this definition, then replace this position and then continue traversing the end of the loop to output this value is the maximum value or the smallest value such as
Double max = score[0]; for (i = 0; i < score.length; i++) { if (Score[i] > Max) { //here the greater than sign becomes less than the number can be the minimum value max = Score[i]; } } SYSTEM.OUT.PRINTLN (i + "Student's highest score is" + max);
This code is to ask for an extremum and the largest one.
Find
The function is to query the current array for the value you need, as if you were going to a place to find someone.
The lookup uses the Boolean variable because the purpose of this function is to find and not find a change in the word is found is true (or false) did not find is false (or true)
If judgment is written inside the For loop
The method of judging is that when the user enters a value that is the same as a value in the array, then the output is not found (else) output is not output, whatever you want.
And then you can do that with a Boolean, and if that's true then jump out of the loop and go into the next loop because the Boolean changes to True when it jumps out, so in the following if it appears in real form and output
For example
Boolean flag = false; System.out.println ("Please enter the score you are looking for:"); Double findscore = input.nextdouble (); for (i = 0; i < score.length; i++) { if (score[i] = = Findscore) { flag = true;//is true break;//jumps out of this for loop into the following I F Loop } } if (flag) {//Because the above Boolean becomes true, enter this if if not found, jump knife under the else System.out.println ("Find the score you are looking for.") "+ score[i]); } else { System.out.println ("The score you are looking for is not found. ");
Inverted
Upside down means that the last element becomes the first element and the first element becomes the last.
The process is to compromise half
Do not traverse the entire array to traverse half, then/2 can be but the computer number method is starting from 0, that is, the last value inside is meaningless, so to the last value that-1
And also to define an empty variable because it takes three steps to change the value (variable) is not defined can also
For example
for (i = 0; i < SCORE.LENGTH/2; i++) { Double temp = score[i];//Here is an empty variable assignment three step assignment the first step score[i] = Score[score.leng Th-1-i];//The value on the right is assigned to the left, then the right side becomes empty value score[score.length-1-i] = temp;//Ibid but this temp is a temporary value so he's finally empty. No } for (i = 0; i < score.length; i++) { System.out.println (score[i]);
Sort
sort is a reference to a class of Java comes with so no difficulty For example
Arrays.sort (score);//This code is a sorted class for (i = 0; i < score.length; i++) { System.out.println (score[i]); }
Insert
Inserting means inserting a variable (value) somewhere in the array.
But once this variable is inserted, it will change the array so the array must be empty of the extra space.
< Span style= "font-family:"microsoft yahei"" >< Span style= "font-size:24px" > and to judge the inserted data, if the judgment ends, the
The condition of the judgment is that the input value is greater than the value in the array and is less than the value in front of the value (num[i]-1) to replace this value jumps into the next loop
For example
int[] num = new INT[6]; for (i = 0; i < num.length; i++) { Num[i] = Input.nextint (); } Arrays.sort (num); System.out.println ("Enter the number you want to insert:"); int insert = Input.nextint (); for (i = 0; i < num.length; i++) { if ((Insert > Num[i]) && (Insert < num[i-1]) { Num[i] = Ins ert; break; } } for (i = 0; i < num.length; i++) { System.out.println (num[i]); }
Java Basics-7 algorithms for arrays (sort, sum, max, traverse ...) )