java中Array(数组)的用法

8.Array(数组)   

数组是作为对象来实现的。(really occupy the memopry,真实的占用内存 )

An array is a data structure that stores a collection of value of the same type.(数组是一个数据结构,它存储一堆类型相同的值)

/*下面这句话只是宣称了一个参考类型的变量,而并没有真正初始化,this statement just declares the reference type variables, not yet initialize*/
int[] a;
string[] b;
char[] c;
/*下面这句话真正初始化了,也就是真正给数组分配了内存,Initialize: Allocate memory for the array.*/
a = new int[10];
b = new String[3];
c = new char[20];

int[] a;
a = new int[3];
a[0] = 1;
a[1] = 2;
a[2] = 3;
System.out.println("the value of a[1] = " + a[1]);
a[1] = 100;
System.out.println("the value of a[1] = " + a[1]);

Initailizer(以下为另一种初始化数组的方法)
int[] a = {1,3,5,7,9};
Array length(数组的长度)
int i=a.length;//5


举例:
int array_int[ ];
String[ ] str;
利用new 来为数组型变量分配内存空间
array_int=new int[10];
str=new String[10];
两步可以合并,如:
String[ ] str=new String[10];

可以在它的length实例变量中找到一个数组的大小——也就是,一个数组能保存的元素的数目 。
所有的数组都有这个变量,并且它总是保存数组的大小。 

8.1 数组的length

Length:数组的容量,而不是数组实际存储的元素的个数(mark, during initialization, 
the value of the array is initialized to 0, if the array 类型 is integer
)
 

// This program demonstrates the length array member.
class Length {
    public static void main(String args[]) {
        int a1[] = new int[10];
        int a2[] = {3,5,6,1,8,45,44,-10};
        int a3[] = {4,3,2,1};
        a1[1]=8;

        System.out.println("length of a1 is " + a1.length);
        System.out.println("length of a2 is " + a2.length);
        System.out.println("length of a3 is " + a3.length);
    }
}

更多请见: http://www.mark-to-win.com/tutorial/java_1_Array.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值