星空网 > 软件开发 > Java

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




原标题:Java学习小结(1)

关键词:JAVA

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流