好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

简单了解Java方法的定义和使用实现

什么是方法?

System.out.println(),那么它是什么呢?系统类里的,对象out,输出方法println Java方法是语句的集合,它们在一起执行一个功能。 方法是解决一类问题的步骤的有序组合 方法包含于类或对象中 方法在程序中被创建,在其他地方被引用 设计方法的原则:方法的本意是功能块,就是实现某个功能的语句块集合。我们设计方法的时候,最好保持方法的原子性,就是只完成一个功能,这样利于我们后期的扩展。 回顾:方法的命名规则? 练习:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

package com.lxw.method;

public class Demo01 {

     public static void main(String[] args) {

//        int add = add(1, 5);

//        System.out.println(add);

         box();

     }

     public static int add( int a, int b) { //static让这个方法成为类方法,不然无法直接在主方法调用,

                                         // 定义一个方法名字加类型如果是void就没有返回值,如果有就有返回值

         return a + b;

     }

     public static void box() {

         for ( int i = 1 ; i <= 5 ; i++) { //外层循环5次,代表五行

             for ( int j = 5 ; j >= i; j--) {

                 System.out.print( " " );

             }

             for ( int j = 1 ; j <= i; j++) {

                 System.out.print( "*" );

             }

             for ( int j = 1 ; j < i; j++) {

                 System.out.print( "*" );

             }

             System.out.println();

         }

     }

}

方法的定义

Java的方法类似于其他语言的函数,是一段用来完成特定功能的代码片段,一般情况下,定义一个方法包含以下语法: 方法包含一个方法头和一个方法体。下面是一个方法的所有部分: 修饰符:修饰符,这是可选的,告诉编译器如何调用该方法。定义了该方法的访问类型。 public是公共的,所有人都可以使用 它返回值类型:方法可能会返回值。returnValueType是方法返回值的数据类型。有些方法执行所需的操作,但没有返回值。在这种情况下,returnValueType是关键字void。 方法名:是方法的实际名称。方法名和参数表共同构成方法签名。 参数类型:参数像是一个占位符。当方法被调用时,传递值给参数。这个值被称为实参或变量。参数列表是可选的,方法可以不包含任何参数。 形式参数:在方法被调用时用于接收外界输入的数据 实参:调用方法时实际传给方法的数据。 方法体:方法体包含具体的语句,定义该方法的功能。

方法调用 调用方法:对象名.方法名(实参列表) Java支持两种调用方法的方式,根据方法是否返回值来选择。 当返回一个值的时候,方法调用通常被当作一个值。例如:

?

1

int larger=max( 30 , 40 );

如果方法返回值是void,方法调用一定是一条语句。

?

1

System.out.println( "Hell0,kuangshen!" );

课后拓展了解:值传递(Java)和引用传递

练习 :

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

package com.lxw.method;

public class Demo01 {

     //main方法

     public static void main(String[] args) {

         //实际参数:实际调用传递给他的参数

         int add = add( 1 , 5 );

         System.out.println(add);

 

     }

     //加法

     //形式参数,用来定义作用的

     //public是公共的,所有人都可以使用它

     public static int add( int a, int b) { //如果要直接使用这个方法,static让这个方法成为类方法,不然无法直接在主方法调用,

                                         // 定义一个方法名字加类型如果是void就没有返回值,如果有就有返回值

         return a + b; //如果方法存在返回值的话,一定要用return返回如果是void就不用返回了

     }

}

2021.6.2 方法的重载(重要笔试可能遇到)

重载就是在一个类中,有相同的函数名称,但形参不同的函数。 方法的重载的规则: 方法名称必须相同。 参数列表必须不同(个数不同,或类型不同、参数排列顺序不同等)。 方法的返回类型可以相同也可以不相同。 仅仅返回类型不同不足以成为方法的重载。 实现理论: 方法名称相同时,编译器会根据调用方法的参数个数、参数类型等去逐个匹配,以选择对应的方法如果匹配失败,则编译器报错。

练习:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

package com.lxw.method;

public class Demo02 {

     public static void main(String[] args) {

         int max = max( 50 , 20 );

         System.out.println(max);

     }

     //方法可以无限制重名,但是确保数据类型不同

     public static int max( int num1, int num2){

         int result= 0 ;

 

         if (num1>num2){

             result=num1;

             System.out.println( "num1>num2" );

         } else if (num1==num2){

             System.out.println( "num1==num2" );

             return 0 ; //也可以用来终止方法

         } else {

             result=num2;

             System.out.println( "num1<num2" );

         }

         return result;

     }

//    public static double max(double num1,double num2){

////        int result=0;

////

////

////        if (num1>num2){

////            result=num1;

////            System.out.println("num1>num2");

////        }else if (num1==num2){

////            System.out.println("num1==num2");

////            return 0; //也可以用来终止方法

////        }else {

////            result=num2;

////            System.out.println("num1<num2");

////        }

////        return result;

//    }

     public static int max( int num1, int num2, int num3){

         int result= 0 ;

 

         if (num1>num2){

             result=num1;

             System.out.println( "num1>num2" );

         } else if (num1==num2){

             System.out.println( "num1==num2" );

             return 0 ; //也可以用来终止方法

         } else {

             result=num2;

             System.out.println( "num1<num2" );

         }

         return result;

     }

}

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

package com.lxw.method;

public class Demo01 {

     //main方法

     public static void main(String[] args) {

         //实际参数:实际调用传递给他的参数

         int add = add( 1 , 5 , 6 );

         System.out.println(add);

 

     }

     //加法

     //形式参数,用来定义作用的

     //public是公共的,所有人都可以用它

     public static int add( int a, int b) { //如果要直接使用这个方法,static让这个方法成为类方法,不然无法直接在主方法调用,

                                         // 定义一个方法名字加类型如果是void就没有返回值,如果有就有返回值

         return a + b; //如果方法存在返回值的话,一定要用return返回如果是void就不用返回了

     }

     //方法的重载

     public static int add( int a, int b, int c){

         return a+b+c;

     }

}

命令行传参

有时候你希望运行一个程序时候再传递给它消息。这要靠传递命令行参数给main()函数实现。

?

1

2

3

4

5

6

7

8

package com.lxw.method;

public class Demo03 {

     public static void main(String[] args) {

         for ( int i= 0 ;i<args.length;i++){

             System.out.println( "args[" +i+ "]:" +args[i]);

         }

     }

}

2021.6.3可变参数(也叫不定项参数)

jdk1.5开始,java支持传递同类型的可变参数给一个方法。 在方法声明中,在指定参数类型后加一个省略号(……)。 一个方法中只能指定一个可变参数,它必须时方法的最后一个参数,任何普通的参数必须在它之间声明。 练习:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

package com.lxw.method;

public class Demo04 {

     public static void main(String[] args) {

         //调用可变参数

         printMax( 1 , 2 , 23 , 2 , 5 , 4 );

         printMax( new double []{ 1 , 2 , 3 , 4 , 5 , 6 ,});

         Demo04 demo04 = new Demo04();

         demo04.test( 1 , 2 , 3 , 4 , 5 , 6 );

         demo04.test( 2 , 11 , 22 , 33 , 44 , 55 );

 

     }

     public static void printMax( double ... numbers){

         if (numbers.length== 0 ){

             System.out.println( "你没有输出任何数" );

             return ;

         }

         double result=numbers[ 0 ];

         //排序

         for ( int i = 1 ; i < numbers.length; i++) {

             if (numbers[i]>result){

                 result=numbers[i];

             }

         }

         System.out.println( "最大的值是:" +result);

     }

     public   void test( double a, int ... i){ //可变参数,只能放在最后面

         System.out.println(i[ 0 ]);

         System.out.println(i[ 1 ]);

         System.out.println(i[ 2 ]);

         System.out.println(i[ 3 ]);

         System.out.println(i[ 4 ]);

         System.out.println(i[ 5 ]);

     }

}

递归(重要,不推荐使用)

A方法调用B方法,我们很容易理解! 递归就是:A方法调用A方法!就是自己调用自己 利用递归可以用很简单的程序来解决一些复杂的问题。 它通常把一个大型复杂的问题层层转化为一个与原问题相似的规模较小的问题来求解,递归策略只需少量的程序就可描述出解题过程所需要的多次重复计算,大大减少了程序的代码量。递归的能力在于用有限的语句来定义对象的无限集合。 递归结构包括两个部分: 递归头:什么时候不调用自身方法。如果没有头,将陷入死循环。 递归体:什么时候需要调用自身方法。 程序从前往后运行,值从后往前传递。 想要使用递归,前提是基数较小(像,100,1000,1000...这些就比较大了),基数较大会导致栈溢出!

练习:

作业:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

package com.lxw.method;

import java.util.Scanner;

     public class DiGuiZuoYe01 {

         public static void main(String[] args) {

             Scanner scanner = new Scanner(System.in);

             while (scanner.hasNextDouble()) {

                 double a = scanner.nextDouble();

                 String c = scanner.next();

                 double b = scanner.nextDouble();

                 switch (c){

                     case "+" :

                         add(a,b);

                         break ;

                     case "-" :

                         minus(a,b);

                         break ;

                     case "*" :

                         multiply(a,b);

                         break ;

                     case "/" :

                         except(a,b);

                         break ;

                 }

                 System.out.println( "输入任意字母退出" );

             }

             scanner.close();

         }

         public static void add( double num1, double num2) {

             System.out.println( num1 + num2);

         }

         public static void minus( double num1, double num2) {

             System.out.println( num1 - num2);

         }

         public static void multiply( double num1, double num2) {

             System.out.println( num1 * num2);

         }

         public static void except( double num1, double num2) {

             if (num2== 0 ){

                 System.out.println( "分母不能为0" );

             } else {

                 System.out.println( num1 / num2);

             }

         }

     }

//————————————————

//    版权声明:本文为CSDN博主「ppp_melody」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

//    原文链接:https://blog.csdn.net/ppp_melody/article/details/116515970

//    public static void main(String[] args) {

//

//        Scanner a1 = new Scanner(System.in);

//        Scanner b1 = new Scanner(System.in);

//        int a=a1.nextInt();

//        int b=b1.nextInt();

//        int add = add(a, b);

//        int jian = jian(a, b);

//        int chen = chen(a, b);

//        int chu= chu(a, b);

//        System.out.println("a+b="+add);

//        System.out.println("a-b="+jian);

//        System.out.println("a*b="+chen);

//        System.out.println("a/b="+chu);

//

//    }

//    public static int add(int a,int b){

//        return a+b;

//    }

//    public static int jian(int a,int b){

//        return a-b;

//    }

//    public static int chen(int a,int b){

//        return a*b;

//    }

//    public static int chu(int a,int b){

//        return a/b;

//    }

2021.6.5什么是数组

数组的定义:

数组是相同类型的数据的有序集合 数组描述的是相同类型的诺干个数据,按照一定的先后次序排列组合而成。 其中,每一个数据称作一个数组元素,每个数组元素可以通过一个下标来访问它们。

数组声明创建

首先必须声明数组变量,才能在程序中使用数组。下面是声明数组变量的语法:

?

1

2

3

dataType[] arrayRefVar; //首选的方法

dataType arrayRefVar[]; //效果相同,但不是首选方法

Java语言使用new操作符来创建数组,语法如下:

?

1

dataType[] arrayRefVar= new dataType[arraySize];

数组的元素是通过索引访问的,数组索引从0开始。

获取数组长度:

arrays.lenth

练习:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

package com.lxw.array;

public class ArrayDemo01 {

     //变量的类型 变量的名字 = 变量的值;

     //数组类型

     public static void main(String[] args) {

         int [] nums; //1.数组的定义,声明一个数组

//        int nums1[];//C++与C#里的定义

         nums = new int [ 10 ]; //创建一个数组

         //给数组元素中赋值

         nums[ 0 ]= 1 ;

         nums[ 1 ]= 2 ;

         nums[ 2 ]= 3 ;

         nums[ 3 ]= 4 ;

         nums[ 4 ]= 5 ;

         nums[ 5 ]= 6 ;

         nums[ 6 ]= 7 ;

         nums[ 7 ]= 8 ;

         nums[ 8 ]= 9 ;

         nums[ 9 ]= 10 ;

         int sums= 0 ;

         for ( int i = 0 ; i < nums.length; i++) {

             sums=sums+nums[i];

         }

         System.out.println( "数组的和为:" +sums);

     }

}

2021.6.6 内存分析(重要)

Java内存分析

写代码画图分析内存

三种初始化

静态初始化

?

1

2

int [] a={ 1 , 2 , 3 };

Man[] mans={ new Man( 1 , 1 ), new Man( 2 , 2 )};

动态初始化

?

1

2

3

int [] a= new int [ 2 ];

a[ 0 ]= 1 ;

a[ 1 ]= 2 ;

数组的默认初始化 数组引用类型,它的元素相当于类的实例变量,因此数组一经分配空间,其中的每个元素被按照实例变量同样的方式被隐式初始化。

练习:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

package com.lxw.array;

public class ArrayDemo02 {

     public static void main(String[] args) {

         //静态初始化:创建+赋值

         int [] a={ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 };

         System.out.println(a[ 0 ]);

         //引用类型

         Man[] mans={ new Man(), new Man()};

         //动态初始化:包括默认初始化

         int [] b= new int [ 10 ];

         b[ 0 ]= 10 ;

         b[ 1 ]= 10 ;

         System.out.println(b[ 0 ]);

         System.out.println(b[ 1 ]);

         System.out.println(b[ 2 ]);

         System.out.println(b[ 3 ]);

 

     }

}

下标及越界小结

数组的四个基本特点:

其长度是确定的。数组一旦被创建,它的大小就是不可改变的。 其元素必须是相同类型,不允许出现混合类型。 数组中的元素可以是任何数据类型,包括基本类型和引用类型。 数组变量引用类型,数组也可以看成是对象,数组中的每个元素相当于该对象的成员变量。数组本身就是对象,Java中对象是在堆中的,因此数组无论保存原始类型还是其他对象类型,数组对象本身实在堆中的。 数组的边界: 下标的合法区间:[0,lenth-1],如果越界就会报错:

?

1

2

3

4

public static void main(String[] args){

     int [] a= new int [ 2 ];

     System.out.println(a[ 2 ]);

}

ArrayIndexOutOfBoundsException:数组下标越界异常!

小结:

数组是相同数据类型(数据类型可以为任意类型)的有序集合 数组也是对象。数组元素相当于对象的成员变量 数组长度的确定的,不可改变的。如果越界,则报:ArrayIndexOutOfBounds

练习:

?

1

2

3

4

5

6

7

public static void main(String[] args) {

     //静态初始化:创建+赋值

     int [] a={ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 };

     System.out.println(a[ 0 ]);

     for ( int i = 0 ; i < a.length; i++) {

         System.out.println(a[i]);

     }

2021.6.7数组的使用

普通的for循环 For-Each循环 数组作方法入参 数组作返回值-

练习:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

package com.lxw.array;

public class ArrayDemo03 {

     public static void main(String[] args) {

         int [] a1={ 1 , 2 , 3 , 4 , 5 , 6 };

         //数组遍历一遍

         for ( int i = 0 ; i < a1.length; i++) {

             System.out.println(a1[i]);

         }

         System.out.println( "=========================" );

         //求累加和

         int sum= 0 ;

         for ( int i = 0 ; i < a1.length; i++) {

             sum=sum+a1[i];

         }

         System.out.println(sum);

         System.out.println( "========================" );

         //查找最大元素

         int max=a1[ 0 ];

         for ( int i = 0 ; i < a1.length; i++) {

             if (a1[i]>max){

                max=a1[i];

             }

         }

         System.out.println( "max为:" +max);

     }

}

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

package com.lxw.array;

public class ArrayDemo04 {

     public static void main(String[] args) {

         int [] a={ 1 , 2 , 3 , 4 , 5 , 6 };

//        //jdk1.5,没有下标,适合打印

//        for (int i : a) {

//            System.out.println(i);

//

//        }

         printArray(a);

         int [] reverse=reverse(a);

         printArray(reverse);

     }

     //打印按钮元素

     public static void printArray( int [] a){

         for ( int i = 0 ; i < a.length; i++) {

             System.out.print(a[i]+ " " );

         }

     }

     //反转按钮

     public static int [] reverse( int [] a){

         int [] result= new int [a.length];

         for ( int i= 0 ,j=result.length- 1 ;i< a.length;i++,j--){

             result[j]=a[i];

         }

         return result;

     }

}

二维数组

多维数组可以看成是数组的数组,比如二维数组就是一个特殊的一维数组,其每一个元素都是一个一维数组。 二维数组

int a[][]=new int[2][5];

解析:以上二维数组a可以看成一个两行五列的数组。 思考:多维数组的使用?

num[1][0];

练习:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

package com.lxw.array;

import static com.lxw.array.ArrayDemo04.printArray;

public class ArrayDemo05 {

     public static void main(String[] args) {

         int [][] array={{ 1 , 2 },{ 2 , 3 },{ 3 , 4 },{ 4 , 5 }};

//        printArray(array[0]);

//        System.out.println(array[2][0]);

//        System.out.println(array[2][1]);

         for ( int i = 0 ; i < array.length; i++) {

             for ( int j = 0 ; j < array[i].length ; j++) {

                 System.out.println(array[i][j]);

             }

         }

 

     }

}

2021.6.8 Arrays类

Arrays类 数组工具类java.util.Arrays 由于数组对象本身并没有什么方法可以供我们调用,但API中提供了一个工具类Arrays供我们使用,从而可以对数据对象进行一些基本的操作。 查看JDK帮助文档 Arrays类中的方法都是static修饰的静态方法,在使用的时候可以直接使用类名进行调用,而[不用]使用对象来调用(注意:[是不用]而不是[不能]) 具有以下常用功能: 给数组赋值:通过fill方法。 对数组排序:通过sort方法按升序。 比较数组:通过equals方法比较数组中元素值是否相等。 查找数组元素:通过binarySearch方法能对排序好的数组进行二分查找法操作。

练习:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

package com.lxw.array;

import java.util.Arrays;

public class ArrayDemo06 {

     public static void main(String[] args) {

         int [] a={ 1 , 2 , 3 , 567 , 548 , 788 , 842 , 666 , 777 , 235 };

         System.out.println(a); //[I@4554617c哈希值,地址值

         //toString打印数组元素

         System.out.println(Arrays.toString(a));

         printArray(a);

         Arrays.sort(a); //对数组进行排序

         System.out.println(Arrays.toString(a));

         //Arrays.fill(a,0);//对数组进行赋值

         Arrays.fill(a, 2 , 4 , 0 ); //对数组进行赋值

         System.out.println(Arrays.toString(a));

     }

     public static void printArray( int [] a){

         for ( int i = 0 ; i < a.length; i++) {

             if (i== 0 ){

                 System.out.print( "[" );

             }

             if (i==a.length- 1 ){

                 System.out.print(a[i]+ "]" );

             } else {

                 System.out.print(a[i]+ ", " );

             }

         }

     }

}

冒泡排序

冒泡排序无疑是最为出名的排序算法之一,还有其他八大排序! 冒泡的代码还是相当简单的,两层循环,外层冒泡轮数,里层次比较,江湖中人尽皆知。 我们看到嵌套循环,应该立马就可以得出这个算法的时间复杂度为O(n2)。 思考:如何优化?

练习 :

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

package com.lxw.array;

import java.util.Arrays;

public class ArrayDemo07 {

     //冒泡排序

     //1.比较数组中,两个相邻的元素,如果第一个数比第二个数大,我们就交换它们的位置

     //2.每一次比较,都会产生一个最大,或者最小的数字

     //3.下一轮则可以少一次排序!

     //4.依次循环,直到结束!

     public static void main(String[] args) {

         int [] a={ 1 , 4 , 5 , 2 , 7 , 9 , 78 , 44 };

         int [] sort = sort(a); //调用完我们自己写的排序方法后,返回一个排序后的数组

         System.out.println(Arrays.toString(a));

     }

     public static int [] sort( int [] a){

         //临时变量

         int temp= 0 ;

         //外层循环,判断我们这个要走多久走多少次

         for ( int i = 0 ; i < a.length- 1 ; i++) {

             boolean flag= false ; //通过flag标识位减少没有意义的比较

             //内层循环,比较判断两个数。如果第一个数,比第二个数大,则交换位置

             //i就是走过的次数啊 所以j就不用再去比较i之前已经比较过的了

             for ( int j = 0 ; j <a.length- 1 -i ; j++) {

                 if (a[j+ 1 ]<a[j]){   //a[j+1]后一个数,a[j]前一个数

                     temp=a[j];

                     a[j]=a[j+ 1 ];

                     a[j+ 1 ]=temp;

                     flag= true ;

                 }

             }

             if (flag== false ){

                 break ;

             }

         }

         return a;

     }

}

2021.6.9稀疏数组(复习)

需求:编写五子棋游戏中,有存盘退出和续上盘的功能

分析问题:因为该二维数组的很多值是默认值0,因此记录了很多没有意义的数据。 解决:稀疏数组 稀疏数组介绍: 当一个数组中大部分元素为0,或者为同一值的数组时,可以使用稀疏数组来保存该数组。 稀疏数组的处理方式是: 记录数组一共有几列几行,有多少个不同值 把具有不同值的元素和行列及值记录在一个小规模的数组中,从而缩小程序的规模

如下图:左边是原始数据,右边是稀疏数组

总结

本篇文章就到这里了,希望能给各位带来帮助,也希望您能够多多关注的更多内容!

原文链接:https://HdhCmsTestcnblogs测试数据/snbg/p/14882772.html

查看更多关于简单了解Java方法的定义和使用实现的详细内容...

  阅读:17次