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);
		}
	}
}

运行结果:





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