Java学习小结 - 数组的创建与传参

(一)数组的创建

数组的创建包括两部分:数组的申明与分配内存空间。

int score[]=null; //申明一维数组score=new int[3]; 分配长度为3的空间

数组的申明还有另外一种方式:

int[] score=把中括号写在数组名前面

通常,在写代码时,为了方便,我们将两行合并为一行:

int score[3]; 将数组申明与分配内存写在一行

(二)传递参数 

在Java中,数组是按照地址引用的方式来传参的,即我们传递的是数组的首地址,能直接对数组进行更改。主要有3点:

    · 实参为数组名;

    · 形参为新申明的数组,如果有返回值,需在函数的类型后加中括号“[]”;

    · 返回值为数组名。

例子如下:

/**
 * Created by lijiaman on 2016/9/16. */public class createArray2
{static void main(String[] args)
    {null;
        score=int[3];int speed[]={12,35};for(int x=0;x<3;x++)
        {
            score[x]=x*2+1;
        })
        {
            System.out.println(score[+x+]=+score[x]);
        }
        System.out.println(length:+score.length);int x=0;x<speed.length;x++)
        {
            System.out.println(Speed:+speed);
        }
    }
}

相关文章

摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠...
摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠...
今天犯了个错:“接口变动,伤筋动骨,除非你确定只有你一个...
Writer :BYSocket(泥沙砖瓦浆木匠)微 博:BYSocket豆 瓣:...
本文目录 线程与多线程 线程的运行与创建 线程的状态 1 线程...