首页 > 编程语言 > 详细

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

时间:2016-09-16 19:35:08      阅读:98      评论:0      收藏:0      [点我收藏+]

(一)数组的创建

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

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);
        }
    }
}

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

原文:http://www.cnblogs.com/lijiaman/p/5876889.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!