这里有新鲜出炉的Java设计模式,程序狗速度看过来!
java 是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由Sun Microsystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaEE(j2ee), JavaME(j2me), JavaSE(j2se))的总称。
这篇文章主要介绍了Java中数组的创建与传参方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下
(一)数组的创建
数组的创建包括两部分:数组的申明与分配内存空间。
- 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中数组的创建与传参方法,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
来源: http://www.phperz.com/article/17/1116/359944.html