java基础(2)方法与数组

1、方法

定义格式:
public static void 方法名() {
方法体
}
调用格式:
方法名();

public class Demo01Method {
    public static void main(String[] args) {
        for (int j = 0; j < 5; j++) {
            for (int i = 1; i<=20; i++){
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

返回值类型为void的方法没有返回值 只能单独调用

public class Demo03MethodReturn {
    public static void main(String[] args) {
        printMethod(5, 6);
        //System.out.println(printMethod(5,6););		//没有返回值return
    }
    public static void printMethod(int a, int b) {	 //不能return
        int result = a + b;
        System.out.println(result);
    }
}

方法重载

  1. 参数个数不同
  2. 参数类型不同
  3. 参数多类型顺序不同
    与返回值类型,参数名等因素无关
public class Demo04MethodReload {
    public static void main(String[] args) {
        System.out.println(sum(1,2.0));
        System.out.println(sum(1, 2, 3));
        System.out.println(sum(1.0, 2));
        System.out.println(sum(1.0, 2, 3));
    }

    public static int sum(int a, double b) {
        return (int) (a + b);
    }

    public static int sum(int a, int b, int c) {
        System.out.println("三个int参数");
        return a + b + c;
    }

    public static int sum(double a, int b) {
        return (int) (a + b);
    }

    public static int sum(double a, int b, int c) {
        System.out.println("三个int参数");
        return (int) (a + b + c);
    }
}

2、数组

特点

  • 一种引用数据类型
  • 类型统一
  • 数组长度在程序运行期间不可变

2.1、初始化

2.1.1、动态(指定长度):数据类型[] 数组名 = new 数据类型[指定长度]

        int[] arrayA = new int[300];
        double[] arrayB = new double[10];
        String[] arrayC = new String[5];

也可简写

2.1.2、静态(指定内容):数据类型[] 数组名 = new 数据类型[] {元素1, 元素2, …} 自动识别数组长度

		int[] arrayA = new int[] {1, 2, 3, 4};
		String[] arrayB = new String[] {"hello", "world"};

可省略为:数据类型[] 数组名 = {元素1, 元2, …}省略后不能简写

        int[] arryA = {1, 2, 3, 4}

也可拆分为两个步骤

		double[] arrayC;
        arrayC = new double[] {1, 2, 3, 4};

已经确定了内容最好用静态初始化

2.2、java数组的内存划分

2.2.1、5模块

  • 栈(stack)

    存放方法中的局部变量,也是方法运行的唯一位置。一旦超出作用域,立即从栈内存中消失

  • 堆(Heap)

    new出来的对象全在堆中
    堆内存里的内容都有一个地址值:16进制
    堆内存里的数据都有默认值
    整数默认0
    浮点数0.0
    字符‘\u0000’
    布尔false
    引用类型null

  • 方法区(Method Area)

    存储.class相关信息,包含方法 信息

  • 本地方法 (Native Method Stack)

    与操作系统相关

  • 寄存器(PC Register)

2.2.2、一个数组

在这里插入图片描述
进入栈内存:方法区中的main方法加载到stack中,并为main方法在stack中开辟一个内存空间
在stack中main方法的内存空间中定义数据类型
new出来的数组放在heap内存中开辟的空间
变量在栈中,它存储的是堆中的内存地址,这就是java中的引用

2.2.3、两个数组

两个独立的数组
两个独立的数组
两个引用地址相同的数组,相互影响
在这里插入图片描述

2.3、数组操作

  1. 数组长度
System.out.println(arrayA.length);
  1. 数组遍历
		for (int i = 0; i < arrayA.length; i++) {
            System.out.println(arrayA[i]);
        }
  1. 将数组作为方法参数,传给方法
public class Demo03ArrayReverse {
    public static void main(String[] args){
        int[] array = {1, 2, 3, 4, 5};
        printArray(array);
    }

    public static void printArray(int[] array) {
        System.out.println("地址"+array);
    }
}
  1. 数组作为方法的返回值,返回给对象
public class Demo03ArrayReverse {
    public static void main(String[] args){
        int[] result = calculate(1,2,3);
        System.out.println(result[1]);
        System.out.println(result[0]);

    }

    public static int[] calculate(int a, int b, int c) {
        int sum = a + b + c;
        int avg = sum/3;
        int[] arrayA = {sum, avg};
        return arrayA;
    }
}
  1. 数组的复制 (详见java常见API(System类))
    public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length):将数组中指定的数据拷贝到另一个数组中。

数组的拷贝动作是系统级的,性能很高。System.arraycopy方法具有5个参数,含义分别为:

参数序号参数名称参数类型参数含义
1srcObject源数组
2srcPosint源数组索引起始位置
3destObject目标数组
4destPosint目标数组索引起始位置
5lengthint复制元素个数

2.4 二维数组

初始化二维数组

  1. 只分配空间
  2. 只分配二维数组空间
  3. 指定内容同时分配空间
       int[][] a = new int[2][3]; //有两个一维数组,每个一维数组的长度是3
       a[1][2] = 5;  //可以直接访问一维数组,因为已经分配了空间
          
       //只分配了二维数组
       int[][] b = new int[2][]; //有两个一维数组,每个一维数组的长度暂未分配
       b[0]  =new int[3]; //必须事先分配长度,才可以访问
       b[0][2] = 5;
        
       //指定内容的同时,分配空间
       int[][] c = new int[][]{
               {1,2,4},
               {4,5},
               {6,7,8,9}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值