JAVA_SE ----- 基础知识总结-----方法,数组

1:方法

         (1)方法:就是完成特定功能的代码块。

                   注意:在很多语言里面有函数的定义,而在Java中,函数被称为方法。

         (2)格式:

                   修饰符 返回值类型 方法名(参数类型 参数名1,参数类型 参数名2...) {

                            方法体语句;

                            return 返回值;

                   }

 

                   修饰符:public static,private,等

                   返回值类型:就是功能结果的数据类型

                   方法名:就是起了一个名字,方便我们调用该方法。

                   参数类型:就是参数的数据类型

                   参数名:就是变量

                   参数分类:

                            实参:实际参与运算的数据

                            形参:方法上定义的,用于接收实际参数的变量

                   方法体语句:就是完成功能的代码块

                   return:结束方法

                   返回值:就是功能的结果,由return带给调用者。

         (3)两个明确:

                   返回值类型:结果的数据类型

                   参数列表:参数的个数及对应的数据类型

         (4)方法调用

                   A:有明确返回值的方法

                            a:单独调用,没有意义

                            b:输出调用,不是很好,因为我可能需要不结果进行进一步的操作。                                       c:赋值调用.

                   B:void类型修饰的方法

                            a:单独调用

         (6)方法的注意事项

                   A:方法不调用不执行

                   B:方法之间是平级关系,不能嵌套定义

                   C:方法定义的时候,参数是用,隔开的

                   D:方法在调用的时候,不用在传递数据类型

                   E:如果方法有明确的返回值类型,就必须有return语句返回。

         (7)方法重载

                   在同一个类中,方法名相同,参数列表不同。与返回值无关。

                  

                   参数列表不同:

                            参数的个数不同。

                            参数的对应的数据类型不同。

                  

2:数组

         (1)数组:存储同一种数据类型的多个元素的容器。

         (2)特点:每一个元素都有编号,从0开始,最大编号是长度-1。

                 编号的专业叫法:索引

         (3)定义格式

                   A:数据类型[] 数组名;

                   B:数据类型 数组名[];

                  

         (4)数组的初始化

                   A:动态初始化

                            只给长度,系统给出默认值

                           

                            举例:int[] arr = new int[3];

                   B:静态初始化

                            给出值,系统决定长度

                           

                            举例:int[] arr = new int[]{1,2,3};

                            简化版:int[] arr = {1,2,3};

         (5)Java的内存分配

                   A:栈 存储局部变量

                   B:堆 存储所有new出来的

                   C:方法区(面向对象部分详细讲解)

                   D:本地方法区(系统相关)

                   E:寄存器(CPU使用)

                  

                   注意:

                            a:局部变量 在方法定义中或者方法声明上定义的变量。

                            b:栈内存和堆内存的区别

                                     栈:数据使用完毕,就消失。

                                     堆:每一个new出来的东西都有地址

                                         每一个变量都有默认值

                                                        byte,short,int,long0

                                                        float,double0.0

                                                        char'\u0000'

                                                        booleanfalse

                                                        引用类型 null

                                         数据使用完毕后,在垃圾回收器空闲的时候回收。

         (7)数组的常见操作

                   A:遍历

                            方式1:

                                     publicstatic void printArray(int[] arr) {

                                               for(intx=0; x

                                                        System.out.println(arr[x]);

                                               }

                                     }

                                    

                            方式2:

                                     publicstatic void printArray(int[] arr) {

                                               System.out.print("[");

                                               for(intx=0; x

                                                        if(x== arr.length-1) {

                                                                 System.out.println(arr[x]+"]");

                                                        }else{

                                                                  System.out.println(arr[x]+",");

                                                        }

                                               }

                                     }

                   B:最值

                            最大值:

                                     publicstatic int getMax(int[] arr) {

                                               intmax = arr[0];

                                              

                                               for(intx=1; x

                                                        if(arr[x]> max) {

                                                                 max= arr[x];

                                                        }

                                               }

                                              

                                               returnmax;

                                     }

                                    

                            最小值:

                                     publicstatic int getMin(int[] arr) {

                                               intmin = arr[0];

                                              

                                               for(intx=1; x

                                                        if(arr[x]< min) {

                                                                 min= arr[x];

                                                        }

                                               }

                                              

                                               returnmin;

                                     }

                   C:逆序

                            方式1:

                                     public staticvoid reverse(int[] arr) {

                                               for(intx=0; x

                                                        inttemp = arr[x];

                                                        arr[x]= arr[arr.length-1-x];

                                                        arr[arr.length-1-x]= temp;

                                               }

                                     }

                                    

                   D:查表

                                     publicstatic String getString(String[] strArray,int index) {

                                               returnstrArray[index];

                                     }

                   E:基本查找

                            方式1:

                                     publicstatic int getIndex(int[] arr,int value) {

                                               for(intx=0; x

                                                        if(arr[x]== value) {

                                                                 returnx;

                                                        }

                                               }

                                              

                                               return-1;

                                     }

 

你可能感兴趣的:(JAVA_SE ----- 基础知识总结-----方法,数组)