您现在的位置:首页 > >

JAVA 学*课件 之 第六章__数组-2012

发布时间:

第六章 数组
?数组的基本操作 ?数组的参数传递 ?数组的查找、排序 ?Arrays类 ?二维数组

数组基本操作
声明数组
? ? ?

数据类型[ ] 数组名; 兼容C/C++中的格式,但不推荐使用 不分配存储空间,仅创建引用数组的存储地址,初始值null 数组名=new 数据类型[数组大小]; New为数组分配空间 将数组的引用赋值给数组名 数据类型[ ] 数组名=new 数据类型[数组大小];

创建数组
? ? ?

可将声明和创建数组合为一条语句
?

数组大小和默认值
? ?

数组创建后不能再改变大小 长度:数组名.length

数组基本操作
数组下标变量
?

数组名[下标 ]; 数据类型[ ] 数组变量={直接量,……}
double[] mylist={1.1, 2.2, 3 .5};

数组初始化
? ?

单个赋值
Double[] mylist=new double[3]; Mylist[0]=1.1; mylist[1]=2.2; mylist[2]=3.5;

foreach 循环
? ?

不用循环变量就可以顺序的访问整个数组 格式:for(elementType element: arrayRefVar){
//处理每个变量

?

}

Example: Testing Arrays
Objective: The program receives 6 numbers from the user, finds the largest number and counts the occurrence of the largest number entered. Suppose you entered 3, 5, 2, 5, 5, and 5, the largest number is 5 and its occurrence count is 4.
TestArray Run

数组基本操作
数组的复制
?

?

通过数组名复制,只能复制数组的引用,不能 复制整个数组,如list1=list2; 数组内容复制方法
使用循环语句逐个复制数组 使用System类中的静态方法arraycopy
?

arraycopy(sourceArray, srcPos, targetArray, tarPos, length)

使用clone方法复制数组(see chap9)

数组参数传递
给方法传递数组参数
?

Java 使用值传递的方式将实参传递给方法,但 传基本数据类型和传数组有很大的不同:
对于基本数据类型的参数,传递的是实参的值 对于数组类型的参数,参数值是数组的引用,方法 传递的是这个引用

例:

Simple Example
public class Test { public static void main(String[] args) { int x = 1; // x represents an int value int[] y = new int[10]; // y represents an array of int values m(x, y); // Invoke m with arguments x and y System.out.println("x is " + x); System.out.println("y[0] is " + y[0]); } public static void m(int number, int[] numbers) { number = 1001; // Assign a new value to number numbers[0] = 5555; // Assign a new value to numbers[0] } }

public int[] result = new int[list.length];

Returning an Array from a Method list) { static int[] reverse(int[]

for (int i = 0, j = result.length - 1; i < list.length; i++, j--) { result[j] = list[i]; }

list

return result; }

result
int[] list1 = new int[]{1, 2, 3, 4, 5, 6}; int[] list2 = reverse(list1);

从方法中返回数组
public static int[] reverse(int[] list) { int[] result = new int[list.length]; for (int i = 0, j = result.length - 1; i < list.length; i++, j--) { result[j] = list[i]; }

list

return result; }

result
int[] list1 = new int[]{1, 2, 3, 4, 5, 6}; int[] list2 = reverse(list1);

Example: Counting Occurrence of
Each Letter

Generate 100 lowercase letters randomly and assign to an array of characters. Count the occurrence of each letter in the array.

(a ) E x e c u tin g c re a te A r ra y in L in e 6 S ta c k H eap

(b ) A fte r e x itin g c re a te A r ra y in L in e 6 S ta c k H eap

S p a c e re q u ire d fo r th e c re a te A r ra y m e th o d c h a r[] c h a rs : re f S p a c e re q u ire d fo r th e m a in m e th o d c h a r[] c h a rs : re f

A r ra y o f 1 0 0 c h a ra c te rs S p a c e re q u ire d fo r th e m a in m e th o d c h a r[] c h a rs : re f

A r ra y o f 1 0 0 c h a ra c te rs

CountLettersInArray

Run

Arrays类
Java.util.Arrays
?

?

?

?

?

?

sort():排序,整个数组排序和部分排序,重 载 binarySearch(数组,待查找数据):二分查找, 返回下标 Equals(数组名1,数组名2):判断两个数组是 否相等 fill()方法:填充整个数组或部分数组,函数重 载 实验 详细内容见:p151

二维数组
声明:
?

数据类型[ ][ ] 数组名;

创建:
?

数组名=new 数据类型[m][n]
两个下标,第一个表示行,第二个表示列,每 个元素下标从0开始

下标
?

二维数组
长度
? ?

数组名.length:返回的是行的数目 数组名[i].length: 返回的是第i行中的数组个数, 每一行都是一个一维数组

Two-dimensional Array Illustration
0 0 1 2 3 4
matrix = new int[5][5];

1

2

3

4 0 1 2 3 4

0

1

2

3

4 0 1

0
1

1
2

2
3

4

5

6 9 12

7

2 3

7
10

8
11

int[][] array = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12} };

matrix[2][1] = 7;

matrix.length? 5 matrix[0].length? 5

array.length? 4 array[0].length? 3

Declaring, Creating, and Initializing Using Shorthand Notations
You can also use an array initializer to declare, create and initialize a two-dimensional array. For example,
int[][] array = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12} };
int[][] array = new int[4][3]; array[0][0] = 1; array[0][1] = 2; array[0][2] = 3; array[1][0] = 4; array[1][1] = 5; array[1][2] = 6; array[2][0] = 7; array[2][1] = 8; array[2][2] = 9; array[3][0] = 10; array[3][1] = 11; array[3][2] = 12;

Same as

Lengths of Two-dimensional Arrays
int[][] x = new int[3][4];
x x [0 ] x [1 ] x [2 ] x [2 ][0 ] x [2 ][1 ] x [2 ][2 ] x [2 ][3 ] x .le n g th is 3 x [2 ].le n g th is 4 x [1 ][0 ] x [1 ][1 ] x [1 ][2 ] x [1 ][3 ] x [1 ].le n g th is 4 x [0 ][0 ] x [0 ][1 ] x [0 ][2 ] x [0 ][3 ] x [0 ].le n g th is 4

锯齿数组: Ragged Arrays
Each row in a two-dimensional array is itself an array. So, the rows can have different lengths. Such an array is known as a ragged array. For example, int[][] matrix = { {1, 2, 3, 4, 5}, matrix.length is 5 matrix[0].length is 5 {2, 3, 4, 5}, matrix[1].length is 4 {3, 4, 5}, matrix[2].length is 3 {4, 5}, matrix[3].length is 2 {5} matrix[4].length is 1 };

Ragged Arrays, cont.

int[][] triangleArray = { {1, 2, 3, 4, 5}, {2, 3, 4, 5}, {3, 4, 5}, {4, 5}, {5} };

1 2 3 4 1 2 3 4 1 2 3 1 2 1 2

5

Example: 多选题评分
S tu d e n ts ’ A n s w e rs to th e Q u e s tio n s :

0 1 2 3 4 5 6 7 8 9 Student Student Student Student Student Student Student Student 0 1 2 3 4 5 6 7 A D E C A B B E B B D B B B B B A A D A D E A E C B A E C C C C C C C D C C C C D A B C D D D D E E E E E E E E E E E E E E E E A A A A A A A A D D D D D D D D

Objective: write a program that grades multiple-choice test.
K e y to th e Q u e stio n s:

0 1 2 3 4 5 6 7 8 9 Key D B D C C D A E A D

GradeExam

Run

作业
复*题
?

6.5 6.11 6.13 6.27

编程题
?

6.1 6.7 6.20



热文推荐
猜你喜欢
友情链接: