java9:一維數組的聲明,創建,初始化(single-dimensional arrays)

1 聲明與定義:

在說數組之前,還是得先說下聲明與定義:

 變量的定義(definition):用於爲變量分配存儲空間,還可以爲變量指定初始值。在一個程序中,變量有且僅有一個定義;
 變量的聲明(declaration):用於向程序表明變量的類型和名字。定義也是聲明:當定義變量時我們聲明瞭它的類型和名字。可以通過使用extern關鍵字聲明變量名而不定義它。

也就是說,有分配空間的叫定義, 沒分配空間的叫聲明。

怎麼理解 聲明中說的”定義也是聲明“,這說明聲明包括定義。所以諸如int a;extern int a;之類的一定是聲明。那是不是定義還要接着往下看;如果程序前面都沒有出現過a這個變量,這時你要使用a,你必須讓程序知道你要使用a這個變量了。這時候你寫入int a;以前沒有a這個變量的,現在程序爲了記住它,就得爲他分配空間,於是這是個定義。
    如果程序包含的其他文件裏已經出現過a了,這證明程序已經爲a分配內存,這時你要使用a就方便很多了。你只需要告訴程序,這個a在其他地方定義過了,於是你寫入extern int a;
    對於int a;來說,它既是定義又是聲明;對於extern int a;來說,它是聲明不是定義。一般爲了敘述方便,把建立存儲空間的聲明稱定義,而不把建立存儲空間的聲明稱爲聲明.

2 聲明一個數組變量 Declaring Array Variables

To use an array in a program, you must declare a variable to reference the array and specify
the array’s element type. Here is the syntax for declaring an array variable:
elementType[] arrayRefVar;

也就是說,爲了在程序中能夠使用數組,你首先要聲明一個變量,該變量是數組的引用,然後同時聲明數組的元素類型, 例如聲明一個double類型的數組,則爲 double [] myList;

這裏要注意,我們這裏聲明的 mylist 他是一個引用,當我們聲明 double [ ] myLis; 時候,我們並沒有爲該數組在內存中分配數組需要的空間,他僅僅分配了這個數組的引用的空間。用一個圖來看

其實就跟我們在講 int a; 既是聲明又是引用一樣,因爲他是基本類型,所以我們這樣聲明時候同時也爲a 分配了空間;但是因爲數組變量是一個引用變量,所以我聲明 double [ ] myList;時候,僅僅分配了該引用的空間,還沒有分配數組的空間。

3 創建數組

 Unlike declarations for primitive data type variables, the declaration of an array variable does
not allocate any space in memory for the array. It creates only a storage location for the refer-
ence to an array. If a variable does not contain a reference to an array, the value of the variable
is null. You cannot assign elements to an array unless it has already been created. After an
array variable is declared, you can create an array by using the new operator with the follow-
ing syntax:

arrayRefVar = new elementType[arraySize];

This statement does two things: (1) it creates an array using new elementType[array-
Size]; (2) it assigns the reference of the newly created array to the variable arrayRefVar.

按照我們上面說的 當我們聲明一個數組變量時候,我們僅僅分配了數組引用的空間,並且初始化爲null, 我們通過new 可以來給數組分配空間。語法是:arrayRefVar = new elementType[arraySize];

這個過程做了兩個事情 1) 分配了arraySize個連續的elementType空間,2)將該塊連續空間的引用賦值給arrayRefVar ,例如 接着上面的 double [] myList;   我們可以創建一個 4個元素的數組  myList=new double[4];

所以現在內存圖應該是這樣:


我們可以將上面的兩個步驟可以合在一起 即直接  double [ ]  myList=new double[4];

4 數組下標變量 Array Indexed Variables及數組初始化

The array elements are accessed through the index. Array indices are 0 based; that is, they
range from 0 to arrayRefVar.length-1. In the example in Figure 6.1, myList holds ten
double values, and the indices are from 0 to 9.
Each element in the array is represented using the following syntax, known as an indexed
variable:
arrayRefVar[index] 

這跟我們在其他語言中一樣,數組的元素通過數組下標來訪問,下標從0~ refvar.length-1; 

例如我們可以給我們剛剛分配的圖初始化:

myList[0]=0;

myList[1]=1;

 myList[2]=2;

myList[3]=3;

另外,java中有簡潔的擊發,叫數組初始化

Java has a shorthand notation, known as the array initializer, which combines in one state-
ment declaring an array, creating an array, and initializing, using the following syntax:
elementType[] arrayRefVar = {value0, value1, ..., valuek};
For example,
double[] myList = {1.9, 2.9, 3.4, 3.5};
This statement declares, creates, and initializes the array myList with four elements, which is
equivalent to the statements shown below:
double[] myList = new double[4];
myList[0] = 1.9;
myList[1] = 2.9;
myList[2] = 3.4;
myList[3] = 3.5;

也就是說  double [ ] myList={1.9,2.9,3.4,3.5}; 這樣一個語句做了三件事情 1)聲明瞭一個數組變量  2)創建了數組元素空間 3)初始化。

5 practice: 紙牌遊戲

假設現在有一副牌52張牌,填着0~51;其中 0~12、13~25、26~38、39~51 分別表示13張黑桃、紅桃、方塊、梅花。現在現將牌打亂,然後從中挑出四張牌,carNumber/13表示該牌屬於哪一種花色,carNumber%13 表示該牌在該花色中是第幾張牌。

這裏說一下,將牌打亂其實就是產生兩個隨機數下標,然後交換這兩個下標對應的元素,這樣就相當模擬了洗牌的過程。

public class DeckOfCard
{
	public static void main(String [] args)
	{
		int [] cards=new int[52];
		String[] suits={"Spandes","Hearts","Diamonds","Clubs"};
		String [] ranks={"Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"};
		//數組初始化
		for(int i=0;i<cards.length;i++)
		{
			cards[i]=i;
		}
		//打亂牌
		for(int i=0;i<cards.length;i++)
		{
			int index=(int)(Math.random()*cards.length);//產生[0,52)之間一個隨機數
			int temp=cards[i];
			cards[i]=cards[index];
			cards[index]=temp;
		}
		for(int i=0;i<cards.length;i++)
		{
			System.out.print(cards[i]+"  ");
			if((i+1)%10==0) System.out.println();
		}
		System.out.println();
		for(int i=0;i<4;i++)
		{
			int index=(int)(Math.random()*cards.length);
			String suit=suits[cards[index]/13];
			String rank=ranks[cards[index]%13];
			System.out.println("index="+index+" card="+cards[index]+" in suit="+suit+", in rank = "+rank);
		}
	}
}

運行結果:





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章