1 /**************************************************
2 *
3 * CH-2-02, 2013-1-14
4 *
5 * 数组使用基础
6 ***************************************************/
7
8
9 /**************************************************
10 *
11 *一、数组
12 * 1、数组的定义
13 * 1)、数据类型 [] 数组名 = new 数据类型[长度];
14 * 2)、数据类型 [] 数组名 = new 数据类型[] {初始值1,初始值2,...};//长度有内容个数决定
15 * 3)、数据类型 [] 数组名 = {初始值1,初始值2,...};//长度有内容个数决定
16 * 2、数组的使用
17 * 3、数组的长度获取
18 * 4、debug调试程序(断点选取)
19 *二、二维数组
20 * 1、定义数组(两种方法)
21 * 2、数组的使用(赋值长采用循环)
22 * 3、数组的长度获取
23 ***************************************************/
24 public class Day4{
25 public static void main(String [] args){
26 //int age[] = null;//还没有分配内存,如果让他指向null使用的时候就会报空指针异常
27 //age[0] = 2;//这里会报空指针异常
28 String[] names = new String[4];
29
30 names[0] = "张三";
31 names[1] = "李四";
32 names[2] = "王五";
33 names[3] = "赵六";
34 //names[4] = "王麻子";//会越界
35
36 //数组名.length 可以获取数组的长度
37 System.out.println("最后一个人的名字" + names[names.length - 1]);
38
39 for(int i = 0; i < names.length; i++){
40 System.out.print(names[i] + " ");
41 }
42 System.out.println();
43
44 sortArray();//调用排序
45 }
46
47 public static void sortArray(){
48 int [] score = {23, 45, 76, 20, 100, 54, 78, 90,66};
49
50 for(int i = 0; i < score.length; i++){
51 System.out.print(score[i] + " ");
52 }
53 System.out.println();
54
55 for(int i = 0; i < score.length; i++){
56 for(int j = i; j < score.length; j++){
57 if(score[i] > score[j]){
58 int temp;
59 temp = score[i];
60
61 score[i] = score[j];
62 score[j] = temp;
63 }
64 }
65 }
66
67 for(int i = 0; i < score.length; i++){
68 System.out.print(score[i] + " ");
69 }
70 System.out.println();
71 }
72
73 public static void multiArray(){
74
75 //定义方法一
76 int [][]ages = new int[3][];//行不可以省略
77 ages[0] = new int[3];
78 ages[1] = new int[3];
79 ages[2] = new int[3];
80 //定义方法二
81 int [][]ages = new int[3][3];//行列确定
82 }
83 }