4--黑马程序员--技术总结之数组

<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,};   //静态初始化,已经赋好初始值

代码示例

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public class ArrayDemo1 {  
  2.     /**  
  3.      * 使用数组的创建的两种方式并打印输出  
  4.      * @黑马ZWF  
  5.      */  
  6.     public static void main(String[] args) {  
  7.         int[] arr1 = new int[5];  
  8.         for(int x = 0; x < arr1.length; x++) {  
  9.             System.out.println(arr1[x]);     //输出结果为0,0,0,0,0 因为没有赋初始值  
  10.         }  
  11.         int[] arr2 = new int[]{1,2,3,4,5,6,7,8,9,0};  
  12.         for(int x = 0; x < arr2.length; x++) {  
  13.             System.out.println(arr2[x]);    //输出结果为1,2,3,4,5,6,7,8,9,0  
  14.         }  
  15.     }  
  16. }  

三.数组的应用
      1)数组的遍历(for循环的另一种用法foreach)

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public class ArrayDemo1 {  
  2.     /**  
  3.      * 使用for循环的另一种方法遍历数组并打印输出  
  4.      * @黑马ZWF  
  5.      */  
  6.     public static void main(String[] args) {  
  7.         int[] arr = new int[]{1,2,3,4,5,6,7,8,9,0};  
  8.         for(int x:arr) {  
  9.             System.out.print(x);    //输出结果为1234567890  
  10.         }  
  11.     }  
  12. }  

        2)获取最值

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public class ArrayDemo2 {  
  2.     /**  
  3.      * 遍历数组或缺数组的最大值并打印输出  
  4.      * @黑马ZWF  
  5.      */  
  6.     public static void main(String[] args) {  
  7.         int[] arr = new int[]{24,13,12,45,78,06,43};   
  8.         int temp = 0;     //定义中间变量并赋初值  
  9.         for(int x = 0; x < arr.length; x++) {  
  10.             if(arr[x] > temp) {  
  11.                 temp = arr[x];  
  12.             }  
  13.         }  
  14.         System.out.println("该数组的最大值是" + temp); //输出结果为:该数组的最大值是78  
  15.     }  
  16. }  


        3)数组排序(冒泡排序和选择排序

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public class ArrayDemo3 {  
  2.     /**  
  3.      * 用冒泡法和选择排序法实现对数组的排序并打印输出  
  4.      * @黑马ZWF  
  5.      */  
  6.     public static void main(String[] args) {  
  7.         // TODO Auto-generated method stub  
  8.         int[] arr = new int[]{213,432,645,752,234,123};  
  9.         bubbleSort(arr);      //输出结果为:123 213 234 432 645 752   
  10.         System.out.println();  
  11.         selectSort(arr);      //输出结果为:752 645 432 234 213 123   
  12.     }  
  13.     public static void bubbleSort(int[] arr) {     //冒泡排序法  
  14.         for(int x = 0; x < arr.length - 1; x++) {     //最多进行arr.length-1次遍历  
  15.             for(int y = 0; y < arr.length - 1 - x; y++) {  
  16.                 if (arr[y] > arr[y + 1]) {    //从小到大输出  
  17.                     change(arr, y, y+1);  
  18.                 }  
  19.             }  
  20.         }  
  21.         printArray(arr);  
  22.     }  
  23.       
  24.     public static void selectSort(int[] arr) { //选择排序法  
  25.         for(int x = 0; x < arr.length - 1; x++) {  
  26.             for(int y = x + 1; y < arr.length; y++) {  
  27.                 if (arr[x] < arr[y]) {       //从大到小输出  
  28.                     change(arr, x, y);  
  29.                 }  
  30.             }  
  31.         }  
  32.         printArray(arr);  
  33.     }  
  34.       
  35.     public static void printArray(int[] arr) { //打印数组  
  36.         for(int x = 0; x < arr.length; x++) {  
  37.             System.out.print(arr[x]+" ");  
  38.         }  
  39.     }  
  40.       
  41.     public static void change(int[] arr, int x, int y){ //交换数据的方法  
  42.             int temp = arr[x];  
  43.             arr[x] = arr[y];  
  44.             arr[y] = temp;        
  45.     }  
  46. }  


         4)数组的查找

         

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public class ArrayDemo4 {  
  2.   
  3.     /**  
  4.      * 在数组中查找指定的值并打印输出  
  5.      * @黑马ZWF  
  6.      */  
  7.     public static void main(String[] args) {  
  8.         // TODO Auto-generated method stub  
  9.         int[] arr = new int[]{123,324,54,6,89,634,64};  
  10.         seekNumber(arr,64);    //输出结果是:该数字在此数组的第7个位置  
  11.         seekNumber(arr,23);    //输出结果是:在此数组中没有没有找到该数字!  
  12.     }  
  13.     public static void seekNumber(int[] arr,int a) {  
  14.         int temp = 0;   //设置中间变量判断是否查找到该数字  
  15.         for(int x = 0; x < arr.length; x++) {  
  16.             if(arr[x] == a) {       //循环遍历,查找是否有目的数字  
  17.                 x++;  
  18.                 temp = x;  
  19.             }     
  20.         }  
  21.         if (temp !=0){  
  22.             System.out.println("该数字在此数组的第" + temp + "个位置" );  
  23.         }  
  24.         else {  
  25.             System.out.println("在此数组中没有没有找到该数字!");  
  26.         }  
  27.     }  
  28. }  


       5)字符数组的应用

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public class ArrayDemo5{  
  2.     /**  
  3.      * 判断一个字符串是否是对称字符串  
  4.      * 例如"abc"不是对称字符串,"aba"、"abba"、"aaa"、"mnanm"是对称字符串  
  5.      * @黑马ZWF  
  6.      */  
  7.     public static void main(String argu[]){  
  8.           
  9.         String[] str = {"abc","aba","abba","aaa","mnanm"};  //定义字符串数组  
  10.         for(int i = 0; i < str.length; i++){  
  11.             System.out.println(str[i] + " is " + symmetry(str[i])); //调用symmetry方法判断并输出结果  
  12.         }  
  13.     }     
  14.     
  15.     public static boolean  symmetry(String str) {   //判断字符串是否对称的方法  
  16.           
  17.         if (null == str) {     
  18.              return  false;     
  19.         }     
  20.           
  21.         for (int i = 0; i < str.length() / 2; i++) {     
  22.                 if (str.charAt(i) != str.charAt(str.length() - i-1)) {     
  23.                 return false;     
  24.             }   //遍历比较,从字符串中间开始比较两边的字符是否相等,有不等的就不是对称的  
  25.         }     
  26.         return true;   //遍历比较完后,没有不相等的字符,即为对称字符串,返回true值  
  27.     }  
  28. }  
           6)进制转换
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public class ArrayDemo6 {  
  2.   
  3.     /**利用数组存储数据进行进制之间的转换  
  4.      * @黑马ZWF
  5.      */  
  6.     public static void main(String[] args) {  
  7.         // TODO Auto-generated method stub  
  8.         toBinaryNumber(16);     //输出结果为:10000  
  9.         toHexNumber(125);  
  10.     }  
  11.     public static void toBinaryNumber(int num) {  //十进制转二进制  
  12.         StringBuffer sb = new StringBuffer();    //利用StringBuffer容器存储数据  
  13.         while (num > 0){  
  14.             sb.append(num % 2);  
  15.             num = num / 2;  
  16.         }  
  17.         System.out.println(sb.reverse());   //将容器里的数据反向输出  
  18.     }  
  19.     public static void toHexNumber(int num) {         
  20.         char[] chs = {'0','1','2','3',  
  21.                       '4','5','6','7',  
  22.                       '8','9','A','B',  
  23.                       'C','D','E','F'};   //定义一个表,十六进制里面的元素分别是0-15  
  24.         char[] arr = new char[8];   //定义一个数组,目的是把每次这个数与完15后的值,进行存储    
  25.         int pos = arr.length-1; //定义一个指针,用于操作数组  
  26.         if (num == 0){  
  27.             System.out.println(num);  
  28.             return;  
  29.         }         
  30.         while (num != 0){   //当num不等于0的时候我们就让这个数不断与上15,和不断的想右移动四位  
  31.               
  32.           
  33.             int temp = num & 15;    //用一个变量记录住这个数与上15的结果  
  34.                                     //temp里面记录的结果肯定是在0到15之间,  
  35.                                     //所以把temp当作定义表的角标值去找对应的元素  
  36.                                     //然后赋值到定义的数组中存储  
  37.             arr[pos] = chs[temp];   //上述操作是获取一个int数的32                    
  38.                                     //个二进制位的最低的四位,为了获取下一个有效四位  
  39.                                     //让这个数再向右移动四位  
  40.             pos--;  
  41.             num = num >>> 4;     
  42.         }  
  43.         for (int x=pos+1;x < arr.length ;x++ ){      //把新的数组进行遍历输出  
  44.             System.out.print(arr[x] + "");  
  45.         }  
  46.     }  
  47. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值