Java Array 数组

[b]数组是对象,是相同类型元素的集合。声明数组变量时的数组类型(即type[])中的type可以是任意数据类型,包括基本类型和引用类型。声明后数组的元素数据类型type便确定下来,该数组只能存放该类型的元素。数组创建后其长度就确定下来,且长度是固定、不可变的。[/b]

[b]数组的length,是属性,String的length()是方法,为防止记混,可做如下推想:取长度的方式,无论是String(length())、集合类(size())、File(length())等,都是通过方法调用来取的。数组在java中是特殊的,因为在jdk中你连它的源码都看不到,据说实现自jvm底层。既然数组特殊,那人家的取长度方式也与众不同,就说得过去了。[/b]

[color=red][b]数组变量的声明、数组对象的创建和数组元素的初始化(Declaring/Creating/Initializing)[/b][/color]
[b]数组变量的声明(常简称为数组的声明) Declaring:[/b]格式为 [color=red]数组类型+数组名称[/color]
数组类型写做[b]type[][/b],其中type是该数组所含元素的数据类型。数组变量声明并没有实际创建数组,只是告诉编译器该数组变量会引用一个包含指定类型元素的数组对象。数组变量的声明中不可以包含任何的长度信息(即[]中为空)!原因是长度是Heap中的数组对象的属性,数组变量不存在长度这一说(只是个存于栈中的指向Heap中实际数组对象的引用)。声明后的数组变量是可以指向任何可达的Heap中同类型数组对象的,这些数组对象的长度都是固定的,但不一定相等。
[b]数组对象的创建 Creating:[/b]
创建数组对象时,其长度必须指定(SIZE OF ARRAY MUST BE SPECIFIED WHEN WE CREATE IT)!其长度或者通过array initializer(eg.[b]{1,2,4}[/b])或者通过dimension expression(eg.[b][int][/b])确定。
两种方式创建数组对象(JLS10.3:[url]http://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html#jls-10.3[/url]):
[color=red][b]1[/b] array creation expression(即new的方式)[/color](jls15.10:[url]http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.10[/url])
[color=red]格式:[b]new type Dim ArrayInitializer[/b] 或 [b]new type DimExpr[/b][/color][quote]说明:两种格式的区别是:前一种创建数组对象,并对数组元素做了初始化;后一种只是创建。
Dim指dimension:维度,即[]..
ArrayInitializer指array initializer
DimExpr指dimension expressions:dimension里加可以转化为int类型的常量,如:[5]、[i](i为int),dimension expressions确定了数组对象的长度。
[/quote]
这里需要注意的是, 不存在格式为“new type DimExpr ArrayInitializer”的数组创建格式,原因是dimension expressions和array initializer都可以确定数组的长度,所以同时使用是不允许的。
int[] ia1 = new int[3];//OK
int[] ia2 = new int[]{1,2,3};//OK
int[] ia3 = new int[]; //compile error:创建数组时必须指定长度length
int[] ia4 = new int[5]{1,2,3,4,5}; //compile error:Cannot define dimension expressions when an array initializer is provided

[color=red][b]2[/b] array initializer[/color] (JLS10.6:[url]http://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html#jls-10.6[/url])
[color=red]格式简单举例(int[]为例):[b]{1,2,3}[/b][/color]
array initializer必须用在数组变量的声明中,或作为array creation expression的组成部分。使用array initializer的方式创建数组对象,其实包含了数据对象的创建和数组元素的初始化。数组对象的长度length等于array initializer的{}中的元素数量。
//正确:在数组变量声明中使用array initializer
int[] ia4 = {1,2,3,4,5};

//正确:array initializer作为array creation expression的组成部分
int[] ia5;
ia5 = new int[]{1,2,3,4,5};

//不正确:array initializer必须用在数组变量声明中
int[] ia6;
ia6 = {1,2,3,4,5}; //Compile error:Arrays constants can only be used in initializers error

[b]数组元素的初始化 Initializing:[/b]
创建数组对象时只要使用了array initializer {} (数组变量的声明中使用也好,作为array creation expression的组成部分也罢),则数组对象的长度由array initializer确定,且[color=red]数组元素的初始化也由array initializer做了[/color]。
未使用array initializer的情况下,编译器使用默认值对数组元素进行初始化。具体默认值,视数组元素类型type,策略同java成员变量的默认初始化:
[url]http://wuaner.iteye.com/admin/blogs/1666376[/url]


一个数组对象可以赋给一个Object引用
Object obj = new int[10];



参阅资料:
[b]
JLS Chapter 10. Arrays:[url]http://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html[/url]
Java Tutorials:[url]http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html[/url]
[/b][quote]An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed.
Like declarations for variables of other types, an array declaration has two components: the array's type and the array's name. An array's type is written as type[], where type is the data type of the contained elements; the square brackets are special symbols indicating that this variable holds an array. The size of the array is not part of its type (which is why the brackets are empty). An array's name can be anything you want, provided that it follows the rules and conventions as previously discussed in the naming section. As with variables of other types, the declaration does not actually create an array — it simply tells the compiler that this variable will hold an array of the specified type.
[/quote]


Java数组浅析
[b][url]http://pengcqu.iteye.com/blog/492188[/url][/b][quote]
数组(array)是相同类型变量的集合,可以使用共同的名字引用它。数组可被定义为任何类型,可以是一维或多维。数组中的一个特别要素是通过下标来访问它。数组提供了一种将有联系的信息分组的便利方法。
注意:如果你熟悉C/C++,请注意, Java数组的工作原理与它们不同。

1、数组不是集合,它[color=red]只能保存[b]同种类型[/b]的多个原始类型或者对象的引用[/color]。数组保存的仅仅是对象的引用,而不是对象本身。

2、数组本身就是对象,Java中对象是在堆中的,因此数组无论保存原始类型还是其他对象类型,数组对象本身是在堆中的。

3、[color=red]数组声明的两种形式:一、int[] arr; 二、int arr[];[/color] 推荐使用前者,这符合Sun的命名规范,而且容易了解到关键点,这是一个int数组对象,而不是一个int原始类型。

4、[color=red]在数组声明中包含数组长度永远是不合法的!如:int[5] arr[/color]; 。因为,声明的时候并没有实例化任何对象,只有在实例化数组对象时,JVM才分配空间,这时才与长度有关。

5、[color=red]在数组构造的时候必须指定长度,因为JVM要知道需要在堆上分配多少空间。反例:int[] arr = new int[];[/color]

6、多维数组的声明。int[][][] arr; 是三维int型数组。

7、一维数组的构造。形如:String[] sa = new String[5];
或者分成两句:String[] sa; sa = new String[5];

8、原始类型数组元素的默认值。对于原始类型数组,在用new构造完成而没有初始化时,JVM自动对其进行初始化。默认值:byte、short、 int、long--0 float--0.0f double--0.0 boolean--false char--'"u0000'。(无论该数组是成员变量还是局部变量)

9、对象类型数组中的引用被默认初始化为null。如:Car[] myCar = new Car[10]; 相当于从myCar[0]到myCar[9]都这样被自动初始化为myCar[i] = null;

10、对象类型的数组虽然被默认初始化了,但是并没有调用其构造函数。也就是说:Car[] myCar = new Car[10];只创建了一个myCar数组对象!并没有创建Car对象的任何实例!

11、多维数组的构造。float[][] ratings = new float[9][]; [color=red][b]第一维的长度必须给出[/b],其余的可以不写,[b]因为JVM需要知道赋给变量ratings的对象的长度[/b]。[/color]

12、数组索引的范围。数组中各个元素的索引是从0开始的,到length-1。每个数组对象都有一个length[color=red][b]属性[/b][/color],它保存了该数组对象的长度。(注意和String对象的length()方法区分开来,这两者没有统一起来是很遗憾的。)

13、Java有数组下标检查,当访问超出索引范围时,将产生ArrayIndexOutOfBoundsException运行时异常。注意,[color=red]这种下标检查不是在编译时刻进行的,而是在运行时!也就是说int[] arr = new int[10]; arr[100] = 100; 这么明显的错误可以通过编译,但在运行时抛出![/color]

Java的数组下标检查是需要额外开销的,但是出于安全的权衡还是值得的,因为很多语言在使用数组时是不安全的,可以任意访问自身内存块外的数组,编译运行都不会报错,产生难以预料的后果!
[/quote]

数组工具类java.util.Arrays的使用:[quote]JDK docs对java.util.Arrays的阐述:This class contains various methods for manipulating arrays (such as sorting and searching). This class also contains a static factory that allows arrays to be viewed as lists.
Arrays.toString举例:
/**
* 输出:
* [I@c17164
* [1, 4, 6, 7, 9]
*/
int[] ia = new int[]{1,4,6,7,9};
System.out.println(ia);
System.out.println(Arrays.toString(ia));
[/quote]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值