<span style="white-space: pre; color: rgb(68, 68, 68); font-family: 'Microsoft Yahei', 微软雅黑, Tahoma, Arial, Helvetica, STHeiti; font-size: 14px; line-height: 21px;"> ----------------------<a target=_blank target="_blank" href="http://www.itheima.com/" style="color: rgb(51, 102, 153); text-decoration: none;">ASP.Net+Unity开发</a></span><span style="white-space: pre; color: rgb(68, 68, 68); font-family: 'Microsoft Yahei', 微软雅黑, Tahoma, Arial, Helvetica, STHeiti; font-size: 14px; line-height: 21px;">、<a target=_blank target="_blank" href="http://www.itheima.com/" style="color: rgb(51, 102, 153); text-decoration: none;">.Net培训</a></span><span style="white-space: pre; color: rgb(68, 68, 68); font-family: 'Microsoft Yahei', 微软雅黑, Tahoma, Arial, Helvetica, STHeiti; font-size: 14px; line-height: 21px;">、期待与您交流! ----------------------</span>
一.数组的定义
所谓数组,就是相同数据类型的元素按一定顺序排列的集合,就是把有限个类型相同的变量用一个名字命名,然后用编号区分他们的变量的集合,这个名字成为数组名,编号成为下标。组成数组的各个变量成为数组的分量,也称为数组的元素,有时也称为下标变量。数组是在程序设计中,为了处理方便, 把具有相同类型的若干变量按有序的形式组织起来的一种形式。这些按序排列的同类数据元素的集合称为数组。
数组的示意图:
数组的格式:
一维数组:
数组元素类型 数组名字[];例如 int arr[];
数组元素类型[] 数组名字;例如 int[] arr;
二维数组:
数组元素类型 数组名字[][];例如 int arr[][];
数组元素类型[][] 数组名字;例如 int[][] arr;
注意事项:1)数组元素的类型可以是任意一种类型,类类型也可以,比如这个数组People china[]; 数组china中可以存放People类型的数据。
2)数组中的各元素是有先后顺序的,它们在内存中按照这个先后顺序连续存放在一起。
3)数组元素用整个数组的名字和它自己在数组中的顺序位置来表示。例如,a[0]表示名字为a的数组中的第一个元素,a[1]代表数组a的第二个元素,以此类推。
二.数组的创建
在Java中,创建数组的格式如下:
格式1:数组元素类型[] 数组名字 = new 数组元素类型[数组元素的个数或数组长度];
例如:int[] arr = new int[10]; //动态初始化,可以随时赋初始值
格式2:数组元素类型[] 数组名字 = new 数组元素类型[]{元素1,元素2,····元素n};
例如:int[] arr = new int[]{1,2,3,4,5,6,7,8,9,0,}; //静态初始化,已经赋好初始值
代码示例
- public class ArrayDemo1 {
- /**
- * 使用数组的创建的两种方式并打印输出
- * @黑马ZWF
- */
- public static void main(String[] args) {
- int[] arr1 = new int[5];
- for(int x = 0; x < arr1.length; x++) {
- System.out.println(arr1[x]); //输出结果为0,0,0,0,0 因为没有赋初始值
- }
- int[] arr2 = new int[]{1,2,3,4,5,6,7,8,9,0};
- for(int x = 0; x < arr2.length; x++) {
- System.out.println(arr2[x]); //输出结果为1,2,3,4,5,6,7,8,9,0
- }
- }
- }
三.数组的应用
1)数组的遍历(for循环的另一种用法foreach)
- public class ArrayDemo1 {
- /**
- * 使用for循环的另一种方法遍历数组并打印输出
- * @黑马ZWF
- */
- public static void main(String[] args) {
- int[] arr = new int[]{1,2,3,4,5,6,7,8,9,0};
- for(int x:arr) {
- System.out.print(x); //输出结果为1234567890
- }
- }
- }
2)获取最值
- public class ArrayDemo2 {
- /**
- * 遍历数组或缺数组的最大值并打印输出
- * @黑马ZWF
- */
- public static void main(String[] args) {
- int[] arr = new int[]{24,13,12,45,78,06,43};
- int temp = 0; //定义中间变量并赋初值
- for(int x = 0; x < arr.length; x++) {
- if(arr[x] > temp) {
- temp = arr[x];
- }
- }
- System.out.println("该数组的最大值是" + temp); //输出结果为:该数组的最大值是78
- }
- }
3)数组排序(冒泡排序和选择排序
- public class ArrayDemo3 {
- /**
- * 用冒泡法和选择排序法实现对数组的排序并打印输出
- * @黑马ZWF
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- int[] arr = new int[]{213,432,645,752,234,123};
- bubbleSort(arr); //输出结果为:123 213 234 432 645 752
- System.out.println();
- selectSort(arr); //输出结果为:752 645 432 234 213 123
- }
- public static void bubbleSort(int[] arr) { //冒泡排序法
- for(int x = 0; x < arr.length - 1; x++) { //最多进行arr.length-1次遍历
- for(int y = 0; y < arr.length - 1 - x; y++) {
- if (arr[y] > arr[y + 1]) { //从小到大输出
- change(arr, y, y+1);
- }
- }
- }
- printArray(arr);
- }
- public static void selectSort(int[] arr) { //选择排序法
- for(int x = 0; x < arr.length - 1; x++) {
- for(int y = x + 1; y < arr.length; y++) {
- if (arr[x] < arr[y]) { //从大到小输出
- change(arr, x, y);
- }
- }
- }
- printArray(arr);
- }
- public static void printArray(int[] arr) { //打印数组
- for(int x = 0; x < arr.length; x++) {
- System.out.print(arr[x]+" ");
- }
- }
- public static void change(int[] arr, int x, int y){ //交换数据的方法
- int temp = arr[x];
- arr[x] = arr[y];
- arr[y] = temp;
- }
- }
4)数组的查找
- public class ArrayDemo4 {
- /**
- * 在数组中查找指定的值并打印输出
- * @黑马ZWF
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- int[] arr = new int[]{123,324,54,6,89,634,64};
- seekNumber(arr,64); //输出结果是:该数字在此数组的第7个位置
- seekNumber(arr,23); //输出结果是:在此数组中没有没有找到该数字!
- }
- public static void seekNumber(int[] arr,int a) {
- int temp = 0; //设置中间变量判断是否查找到该数字
- for(int x = 0; x < arr.length; x++) {
- if(arr[x] == a) { //循环遍历,查找是否有目的数字
- x++;
- temp = x;
- }
- }
- if (temp !=0){
- System.out.println("该数字在此数组的第" + temp + "个位置" );
- }
- else {
- System.out.println("在此数组中没有没有找到该数字!");
- }
- }
- }
5)字符数组的应用
- public class ArrayDemo5{
- /**
- * 判断一个字符串是否是对称字符串
- * 例如"abc"不是对称字符串,"aba"、"abba"、"aaa"、"mnanm"是对称字符串
- * @黑马ZWF
- */
- public static void main(String argu[]){
- String[] str = {"abc","aba","abba","aaa","mnanm"}; //定义字符串数组
- for(int i = 0; i < str.length; i++){
- System.out.println(str[i] + " is " + symmetry(str[i])); //调用symmetry方法判断并输出结果
- }
- }
- public static boolean symmetry(String str) { //判断字符串是否对称的方法
- if (null == str) {
- return false;
- }
- for (int i = 0; i < str.length() / 2; i++) {
- if (str.charAt(i) != str.charAt(str.length() - i-1)) {
- return false;
- } //遍历比较,从字符串中间开始比较两边的字符是否相等,有不等的就不是对称的
- }
- return true; //遍历比较完后,没有不相等的字符,即为对称字符串,返回true值
- }
- }
- public class ArrayDemo6 {
- /**利用数组存储数据进行进制之间的转换
- * @黑马ZWF
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- toBinaryNumber(16); //输出结果为:10000
- toHexNumber(125);
- }
- public static void toBinaryNumber(int num) { //十进制转二进制
- StringBuffer sb = new StringBuffer(); //利用StringBuffer容器存储数据
- while (num > 0){
- sb.append(num % 2);
- num = num / 2;
- }
- System.out.println(sb.reverse()); //将容器里的数据反向输出
- }
- public static void toHexNumber(int num) {
- char[] chs = {'0','1','2','3',
- '4','5','6','7',
- '8','9','A','B',
- 'C','D','E','F'}; //定义一个表,十六进制里面的元素分别是0-15
- char[] arr = new char[8]; //定义一个数组,目的是把每次这个数与完15后的值,进行存储
- int pos = arr.length-1; //定义一个指针,用于操作数组
- if (num == 0){
- System.out.println(num);
- return;
- }
- while (num != 0){ //当num不等于0的时候我们就让这个数不断与上15,和不断的想右移动四位
- int temp = num & 15; //用一个变量记录住这个数与上15的结果
- //temp里面记录的结果肯定是在0到15之间,
- //所以把temp当作定义表的角标值去找对应的元素
- //然后赋值到定义的数组中存储
- arr[pos] = chs[temp]; //上述操作是获取一个int数的32
- //个二进制位的最低的四位,为了获取下一个有效四位
- //让这个数再向右移动四位
- pos--;
- num = num >>> 4;
- }
- for (int x=pos+1;x < arr.length ;x++ ){ //把新的数组进行遍历输出
- System.out.print(arr[x] + "");
- }
- }
- }