The array variable stores the array at 内存中的地址, and the array is a reference type of data, and the address of the variable points to the final data
Just like the array name is the house number, you can only find your home through the house number.
▷ Traversal of arrays
遍历: Access the elements of the array one by one, which can be traversed through for loop
For example, if you don't know how many elements there are in the array, and you want to calculate the sum of the elements,
packagetiny.geeker;publicclassTest{
publicstaticvoidmain(String[] args){
int ages[]={
10,20,30,40};int count =0;for(int i =0; i < ages.length; i++){
count += ages[i];}System.out.println(count);}}
▷ Dynamic initialization of arrays
动态化数组:That is, when defining an array, the specific values of the elements are not stored, but only the data type stored in the array and the length of the array are determined.
Suitable for business scenarios where the specific element values are uncertain at first and only the number of elements is known.
For example, given an array of integers, find the maximum value in it
It is to traverse the array, determine if each value is greater than the previous value, and assign the larger value to the variable.
packagetiny.geeker;publicclassTest{
publicstaticvoidmain(String[] args){
int age[]={
10,20,40,30};int max = age[0];// 定义最开始的值for(int i =0; i < age.length; i++){
if(age[i]> max) max = age[i];}System.out.println("最大值为:"+ max);}}
▷ Array reversal
For example, given an integer array {10, 20, 40, 30}, reverse it into {30, 40, 20, 10}
The principle is to first swap the positions of the first element and the last element, then swap the second element and the penultimate element, and then keep going.
You can define two variablesi和j, where i the default value moves to the right from the first element, j By default, the last element moves to the left, and the positions of the two elements can be swapped every time
▷ debug tools
When writing a program, you can see that the editor has given you error prompts very intuitively.
But if you want to see the changes in values during operation, you need to use the editor's debug tool
First, you need to set a breakpoint on 行号, then right-click and select debug to run, click the run button, and you can see it in the editor The changes in the array are very clear
▷ Method
方法:It is a grammatical structure that encapsulates a piece of code into a function so that it can be called repeatedly
方法It can improve the repeatability of code, improve development efficiency, and also make the logic of the program clearer.
Note: If the method declares a specific return value type, the internal return must return the corresponding data type
packagetiny.geeker;publicclassTest{
publicstaticvoidmain(String[] args){
int count =sum(1,2);System.out.println(count);}publicstaticintsum(int a,int b){
return a + b;}}
▷ Return value
If the defined method does not return a value, then the type of the return value needs to be defined asvoid
If the defined method has a return value, the internal return must return the corresponding data type
There can be multiple formal parameter lists, or even none; if there are multiple formal parameters, they need to be separated by commas, and initialization values cannot be given.
值传递:It means that when transferring actual parameters to formal parameters in a method, what is transferred is a copy of the value stored in the actual parameter variable. Modifying the value of the parameter in the copy will not change the value of the actual parameter.
引用值传递:When transferring to a formal parameter, the address of the actual parameter is passed. After modifying the value, the value in the actual parameter will be changed. For example, an array is passed by reference.
packagetiny.geeker;publicclassTest{
publicstaticvoidmain(String[] args){
// 值传递int a =20;change(a);System.out.println(a);}publicstaticvoidchange(int a){
System.out.println(a);
a =520;// 修改值System.out.println(a);}}