java中Array(數(shù)組)的用法:
8.Array(數(shù)組)
數(shù)組是作為對象來實現(xiàn)的。(really occupy the memopry,真實的占用內(nèi)存 )
馬克- to-win:馬克 java社區(qū):防盜版實名手機尾號: 73203。
An array is a data structure that stores a collection of value of the same type.(數(shù)組是一個數(shù)據(jù)結(jié)構(gòu),它存儲一堆類型相同的值)
/*下面這句話只是宣稱了一個參考類型的變量,而并沒有真正初始化,this statement just declares the reference type variables, not yet initialize*/
int[] a;
string[] b;
char[] c;
/*下面這句話真正初始化了,也就是真正給數(shù)組分配了內(nèi)存,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(以下為另一種初始化數(shù)組的方法)
int[] a = {1,3,5,7,9};
Array length(數(shù)組的長度)
int i=a.length;//5
舉例:
int array_int[ ];
String[ ] str;
利用new 來為數(shù)組型變量分配內(nèi)存空間
array_int=new int[10];
str=new String[10];
兩步可以合并,如:
String[ ] str=new String[10];
可以在它的length實例變量中找到一個數(shù)組的大小——也就是,一個數(shù)組能保存的元素的數(shù)目 。
所有的數(shù)組都有這個變量,并且它總是保存數(shù)組的大小。
8.1 數(shù)組的length
Length:數(shù)組的容量,而不是數(shù)組實際存儲的元素的個數(shù)(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);
}
}
J:\java教程>java Length
length of a1 is 10
length of a2 is 8
length of a3 is 4