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

(一)数组的创建

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

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

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

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

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

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

 

(二)传递参数 

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

    · 实参为数组名;

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

    · 返回值为数组名。

例子如下:

/**
 * Created by lijiaman on 2016/9/16.
 */
public class createArray2
{
    public static void main(String[] args)
    {
        int score[]=null;
        score=new int[3];
        int speed[]={12,35};
        for(int x=0;x<3;x++)
        {
            score[x]=x*2+1;
        }
        for(int x=0;x<3;x++)
        {
            System.out.println("score["+x+"]="+score[x]);
        }
        System.out.println("length:"+score.length);
        for(int x=0;x<speed.length;x++)
        {
            System.out.println("Speed:"+speed);
        }
    }
}
原文地址:https://www.cnblogs.com/lijiaman/p/5876889.html