我的Java——数组(1)

一.一维数组

1.一位数组的定义

type arrayName[];或type [] arrayName;

(java在数组定义中并不为数组元素分配内存 因此[]中不用指出数组中元素个数(即数组长度))

为数组分配内存空间 要用到运算符new

例:int [] score=new int [3];

(数组用new分配空间的同时 数组的每个元素都会自动赋一个默认值(整数为0,实数为0.0,字符为‘\0’,boolean型为false,引用型为null)这是因为 数组实际是一种引用型的变量 而其每个元素是引用型变量的成员变量)

【例】ArrayTest.java 使用数组

public class ArrayTest{

  public static void main(String [] args){

  int  a[]=new int [5];

  for(int i=0;i<a.length;i++)

    a[i]=i;

  for(int i=a.length-1;i>=0;i——)

    System.out.println("a["+i+"]="+a[i]);

}

}

 

posted @ 2016-05-20 09:11  努力成为工程师的芝麻  阅读(110)  评论(0编辑  收藏  举报