没有合适的资源?快使用搜索试试~ 我知道了~
首页Java编程思修第4版本学习笔记(下).pdf
Java编程思修第4版本学习笔记(下).pdf
需积分: 9 448 浏览量
更新于2023-05-29
评论
收藏 2.74MB PDF 举报
Java编程思修第4版本学习笔记(下),总结了Java编程思修这本书16-21章节的内容,我的博客中也有对应的文章供大家阅读~
资源详情
资源评论
资源推荐

第16章:数组
对数组的看法,你可以创建并组装他们,通过使用整形索引值
可以访问他们中的元素,并且他们的尺寸不能发生改变。
1. 数组为什么特殊
数组与其他种类的容器之间的区别有三方面:
1. 效率,数组是效率最高的存储和随机访问对象引用序列的方
式。数组就是一个简单线性序列,使得元素访问非常快速。
代价就是数组对象的大小被固定,并且在生命周期内不可改
变。
2. 类型,泛型之前,其他容器类在处理对象时,都将视作没有
任何类型,也就是说,这些对象都将当作Object处理。数组
之所以优于泛型之前的容器,就是可以创建一个持有具体类
型的数组。
3. 类型,泛型之前,其他容器类在处理对象时,都将视作没有
任何类型,也就是说,这些对象都将当作Object处理。数组
之所以优于泛型之前的容器,就是可以创建一个持有具体类
型的数组。
2.数组是第一级对象
数组标识符其实只是一个引用指向在堆中创建的一个真实对
象,这个数组对象用以保存指向其他对象的引用。对象数组和基本
类型数组的区别主要是:对象数组保存的是引用,基本类型数组保
存的是基本类型的值。

下面我们来讲解数组的几种创建方式:
import java.util.*;
import static net.mindview.util.Print.*;
public class ArrayOptions {
public static void main(String[] args) {
// Arrays of objects:
BerylliumSphere[] a; // Local uninitialized
variable
BerylliumSphere[] b = new BerylliumSphere[5];
// The references inside the array are
// automatically initialized to null:
print("b: " + Arrays.toString(b));
BerylliumSphere[] c = new BerylliumSphere[4];
for(int i = 0; i < c.length; i++)
if(c[i] == null) // Can test for null
reference
c[i] = new BerylliumSphere();
// Aggregate initialization:
BerylliumSphere[] d = { new
BerylliumSphere(),
new BerylliumSphere(), new
BerylliumSphere()
};
// Dynamic aggregate initialization:
a = new BerylliumSphere[]{
new BerylliumSphere(), new
BerylliumSphere(),
};
// (Trailing comma is optional in both cases)
print("a.length = " + a.length);
print("b.length = " + b.length);
print("c.length = " + c.length);
print("d.length = " + d.length);
a = d;
print("a.length = " + a.length);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

// Arrays of primitives:
int[] e; // Null reference
int[] f = new int[5];
// The primitives inside the array are
// automatically initialized to zero:
print("f: " + Arrays.toString(f));
int[] g = new int[4];
for(int i = 0; i < g.length; i++)
g[i] = i*i;
int[] h = { 11, 47, 93 };
// Compile error: variable e not initialized:
//!print("e.length = " + e.length);
print("f.length = " + f.length);
print("g.length = " + g.length);
print("h.length = " + h.length);
e = h;
print("e.length = " + e.length);
e = new int[]{ 1, 2 };
print("e.length = " + e.length);
}
}
运行结果:
b: [null, null, null, null, null]
a.length = 2
b.length = 5
c.length = 4
d.length = 3
a.length = 3
f: [0, 0, 0, 0, 0]
f.length = 5
g.length = 4
h.length = 3
e.length = 3
e.length = 2
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

数组创建的方式:
1. 定义局部变量BerylliumSphere[] a;
2. 定于固定大小的数组对象 BerylliumSphere[] b = new
BerylliumSphere[5];
3. 聚集初始化语法
4. 动态聚集初始化语法
3. 返回一个数组
在Java中,直接返回一个数组,无须担心为数组负责——只要需要
它,就会一直粗壮乃,当用完了垃圾回收器会清理掉它。
4.多维数组
使用花括号将每个向量分隔开:
BerylliumSphere[] d = { new
BerylliumSphere(),
new BerylliumSphere(), new
BerylliumSphere()
};
1
2
3
Arrays.toString(a);1

JavaSE5的Array.deepToString方法,可以将多维数组转换为多个
String:
5. 数组与泛型
通常数组与泛型不能很好地结合。可以参数化数组本身的类型:
public class MultidimensionalPrimitiveArray {
public static void main(String[] args) {
int[][] a = {
{ 1, 2, 3, },
{ 4, 5, 6, },
};
System.out.println(Arrays.deepToString(a));
}
} /*
[[1, 2, 3], [4, 5, 6]]
*/
1
2
3
4
5
6
7
8
9
10
11
public class ThreeDWithNew {
public static void main(String[] args) {
// 3-D array with fixed length:
int[][][] a = new int[2][2][4];
System.out.println(Arrays.deepToString(a));
}
} /*
[[[0, 0, 0, 0], [0, 0, 0, 0]], [[0, 0, 0, 0], [0,
0, 0, 0]]]
*/
1
2
3
4
5
6
7
8
9
class ClassParameter<T> {
public T[] f(T[] arg) { return arg; }
}
class MethodParameter {
1
2
3
4
5
剩余88页未读,继续阅读










安全验证
文档复制为VIP权益,开通VIP直接复制

评论0