1、数组的定义

首先举一个小例自:如果你现在需要定义100个int类型的变量,那么按照前俩节的做法为:

int a = 1, b=2 , c=3.......;

可以发现我们的代码特别的冗余,而且不美观.如果按照这种方式当然可以完成我们的需求,但是定义起来就特别的麻烦.因为这么变量之间并没有关联,都是分开定义的,

此时有个需求,要求将这100个int变量打印一下,那么则意味着要编写写 System.out.println()100次。由此引来我们今天的主题 数组 ~~~

其实所谓的数组指的就是一组相关类型的变量集合,并且这些变量可以按照统一的方式进行操作。数组本身属于引用数据类型,那么既然是引用数据类型,这里面实际又会牵扯到内存分配,而数组的定义语法有如下两类。

语法:

声明并开辟数组:
  数据类型 [] 数组名称 = new 数据类型[长度];
  数据类型 [] 数组名称 = new 数据类型[长度];

那么当数组开辟空间之后,就可以采用如下的方式的操作:

数组的访问通过索引完成,即:“数组名称[索引]”,但是需要注意的是,数组的索引从0开始,所以索引的范围就是0 ~ 数组长度-1,例如开辟了3个空间的数组,所以可以使用的索引是:0,1,2,如果此时访问的时候超过了数组的索引范围,会产生java.lang.ArrayIndexOutOfBoundsException 异常信息;当我们数组采用动态初始化开辟空间后,数组里面的每一个元素都是该数组对应数据类型的默认值;数组本身是一个有序的集合操作,所以对于数组的内容操作往往会采用循环的模式完成,数组是一个有限的数据集合,所以应该使用 for 循环。在 Java 中提供有一种动态取得数组长度的方式:数组名称.length;范例:定义一个int型数组

public class ArrayDemo {
public static void main(String args[]) {
int data[] = new int[3]; /*开辟了一个长度为3的数组*/
data[0] = 10; // 第一个元素
data[1] = 20; // 第二个元素
data[2] = 30; // 第三个元素
for(int x = 0; x < data.length; x++) {
System.out.println(data[x]); //通过循环控制索引
}
}
}

2、使用数组

数组的元素类型和数组的大小都是确定的,所以当处理数组元素时候,我们通常使用基本循环或者 For-Each 循环。

示例:

该实例完整地展示了如何创建、初始化和操纵数组:

public class TestArray {
  public static void main(String[] args) {
     double[] myList = {1.9, 2.9, 3.4, 3.5};      // 打印所有数组元素
     for (int i = 0; i < myList.length; i++) {
        System.out.println(myList[i] + " ");
    }
     // 计算所有元素的总和
     double total = 0;
     for (int i = 0; i < myList.length; i++) {
        total += myList[i];
    }
     System.out.println("Total is " + total);
     // 查找最大元素
     double max = myList[0];
     for (int i = 1; i < myList.length; i++) {
        if (myList[i] > max) max = myList[i];
    }
     System.out.println("Max is " + max);
  }
}

以上实例编译运行结果如下:

1.9
2.9
3.4
3.5
Total is 11.7
Max is 3.5

3、多维数组  多维数组可以看成以数组为元素的数组。可以有二维、三维、甚至更多维数组,但是实际开发中用的非常少。最多到二维数组(学习容器后,我们一般使用容器,二维数组用的都很少)。

public class ArrayDemo {
public static void main(String args[]) {
//此时的数组并不是一个等列数组
int data[][] = new int[][] {
{1, 2, 3}, {4, 5}, {6, 7, 8, 9}};
//如果在进行输出的时候一定要使用双重循环,
//外部的循环控制输出的行数,而内部的循环控制输出列数
for(int i = 0; i < data.length; i++) {
for(int j = 0; j < data[i].length; j++) {
System.out.print("data[" + i + "][" + j + "]=" + data[i][j] + "、");
}
System.out.println();
}
}
}

4、数组的排序:

1 package com.boom0904;
2
3 import java.util.Arrays;
4
5 import javax.sound.sampled.Mixer;
6
7 import com.boom.util.RandomNumber;
8
9 /**
10 * 数组的排序
11 *
12 * @author Administrator
13 *
14 */
15 public class SortTest {
16
17     public static void main(String[] args) {
18         bubbleSort();
19         //selectSort();
20     }
21
22     private static void selectSort() {
23         int arr[] = RandomNumber.createRandomArray(10, 0, 20);
24         System.out.println(Arrays.toString(arr));
25         selectSort(arr);
26         System.out.println(Arrays.toString(arr));
27     }
28
29     private static void bubbleSort() {
30         int arr[] = RandomNumber.createRandomArray(10, 0, 20);
31         System.out.println(Arrays.toString(arr));
32         bubbleSort(arr);
33         System.out.println(Arrays.toString(arr));
34
35     }
36
37     /**
38     * 冒泡排序
39     *
40     * @param arr
41     * @return
42     */
43     public static void bubbleSort(int[] arr) {
44         for (int i = 0; i < arr.length - 1; i++) { // 外层循环控制趟数
45             // boolean flag = false;
46             for (int j = 0; j < arr.length - i - 1; j++) { // 内层循环控制一趟
47                 // 如果前面的的数大于后面的数,定义最大值为前面的数。再进行位置置换,ASC
48                 if (arr[j] > arr[j + 1]) { // arr[j] < arr[j + 1] 反则DESC
49                     // flag = true;
50                     int temp = arr[j];
51                     arr[j] = arr[j + 1];
52                     arr[j + 1] = temp;
53                 }
54             }
55             // if (!flag) {
56             // System.out.println("i==" + i);
57             // break;
58             // }
59         }
60     }
61
62     /**
63     * 选择排序
64     *
65     * @param arr
66     */
67     public static void selectSort(int[] arr) {
68         // 外层循环控制 选择的趟数
69         // i 既可以控制次数,还代表了当前待排序区的第一个元素的索引
70         for (int i = 0; i < arr.length - 1; i++) {
71             // 用于保存当前最小值的下标
72             int minIndex = i;
73             // 对待排序区查找最小值的索引
74             for (int j = i + 1; j < arr.length; j++) {
75                 if (arr[j] < arr[minIndex]) {
76                     minIndex = j;
77                 }
78             }
79             // 将最小值和 下标是i 的元素交换
80             if (i != minIndex) {// 待排序的第一个元素不是最小的情况
81                 int temp = arr[i];
82                 arr[i] = arr[minIndex];
83                 arr[minIndex] = temp;
84             }
85         }
86     }
87
88 }

5、数组的查找:

1 package com.boom0904;
2 /**
3 * 数组的查找
4 */
5 import java.util.Arrays;
6
7 import com.boom.util.RandomNumber;
8
9 public class ArithmeticTest {
10
11     public static void main(String[] args) {
12         // test1();
13         test2();
14     }
15
16     private static void test2() {
17         int[] arr = RandomNumber.createRandomArray(50, 0, 100);
18         Arrays.sort(arr);
19         int index = binarySearch(arr, 99);
20         System.out.println("index=" + index);
21         if (index != -1) {
22             System.out.println(arr[index]);
23         }
24     }
25
26     private static void test1() {
27         int[] arr = RandomNumber.createRandomArray(10, 0, 20);
28         System.out.println(Arrays.toString(arr));
29         System.out.println(indexOf(arr, 9));
30
31     }
32
33     /**
34     * 自定义方法,实现查找指定的数组中是否存在指定的值 时间复杂度:T(n) = O(n)
35     *
36     * @param arr
37     *           被查找的 数组
38     * @param key
39     *           被查找的值
40     * @return 如果 key 在arr 中存在,返回key 的第一个索引,否则返回 -1
41     */
42     public static int indexOf(int[] arr, int key) {
43         // 处理特殊情况:如果数组为空或者数组的长度为0,返回-1
44         if (arr == null)
45             return -1;
46         int len = arr.length;
47         if (len == 0)
48             return -1;
49
50         for (int i = 0; i < len; i++) {
51             if (arr[i] == key)
52                 return i;
53         }
54         return -1;
55     }
56
57     /**
58     * 二分搜索法
59     *
60     * @param arr
61     *           待搜索的数组
62     * @param key
63     *           搜索的值
64     * @return 如果 key 在arr 中存在,返回key 的第一个索引,否则返回 -1
65     */
66     public static int binarySearch(int[] arr, int key) {
67         // 处理特殊情况:如果数组为空或者数组的长度为0,返回-1
68         if (arr == null)
69             return -1;
70         int len = arr.length;
71         if (len == 0)
72             return -1;
73         int low = 0;
74         int high = len - 1;
75         // 不在区间内
76         if (key < arr[low] || key > arr[high])
77             return -1;
78
79         int mid = low + high >> 1;
80         int counter = 0;
81         while (high >= low) {
82             counter++;
83             if (arr[mid] == key) {
84                 System.out.println("找到了。。。查找次数=" + counter);
85                 return mid;
86             } else if (arr[mid] < key) {// 在右边
87                 low = mid + 1;
88             } else { // 在左边
89                 high = mid - 1;
90             }
91             mid = low + high >> 1;
92         }
93         System.out.println("没找到。。查找次数=" + counter);
94         return -1;
95     }
96
97 }
 

												

最新文章

  1. MongoDB的分片(9)
  2. hdu 2177 取(2堆)石子游戏(威佐夫博奕)
  3. oracle数据学习第一天
  4. java基础整理1
  5. Angularjs学习——(一)
  6. Android拍照、录像、录音代码范例
  7. 初学Android 一 基本开发环境
  8. Nodepad ++
  9. Android线控的使用
  10. 模板:LCS(最长公共子序列)
  11. 51nod贪心算法入门-----活动安排问题2
  12. 问题-[Delphi7]程序在WIN7电脑上的日期错误处理
  13. HTML5本地化应用开发-HTML5 Web存储详解
  14. readline函数分析
  15. HTML5和CSS3实例教程[总结二]
  16. linux 版本家族
  17. SSH中的免password登录
  18. Java Web项目中缺少Java EE 6 Libraries怎么添加
  19. Ubuntu18.04问题记录
  20. canvas 模拟时钟

热门文章

  1. Python之介绍、基本语法、流程控制
  2. unity-热更-InjectFix(一)
  3. 数据结构中有关顺序表的问题:为何判断插入位置是否合法时if语句中用length+1,而移动元素的for语句中只用length?
  4. js 或Jquery操作定位元素
  5. 信不信?各种红包App最后都会整合游戏!App+游戏的变现模式分析
  6. 高效C++:构造/析构/赋值
  7. MySQL数据库---表的操作
  8. vue history路由模式 Nginx 生产实践
  9. vue手脚架中使用jq
  10. DQL_MySQL