黑马程序员——JAVA数组

                                -------android培训java培训、期待与您交流! ----------

                    今天,传智老师给我们讲的数组,虽然只讲了数组,但我还是感觉,脑袋装不下,这样排序啊!那样查找啊!好上脑筋哦!但是我不怕,传智豪放哥不是有那句话吗“只要学不死,就往死里学”,我相信只要有努力付出,就会有好的成绩的!虽然这几天都是基础的总结,但我会努力把后面的面向对象等知识不齐并学踏实!come on~!      

数组(重点)
                   (1)存储同一种数据类型的集合。也称为容器。
                   (2)特点:
                       数组会对数据从0开始编号。
                   (3)数组的定义格式
                              A:第一种
                              int[] arr = new int[3];
                              等价:int arr[] = new int[3];
                             B:第二种
                              int[] arr = new int[]{3,1,5,6};
                               变形:int[] arr = {3,1,5,6};

                              使用哪种方式呢?
                              如果数组中的元素很明确,那么就使用第二种的变形形式。
                    (4)java中的内存机制
                             A:栈
                              用于存储局部变量,当数据使用完,所占空间会自动释放。

                              局部变量:定义在函数内的变量。
                            B:堆
                               通过new创建的实体都会在堆内存中。
                             特点:
                             **每一个实体都有内存地址值
                             **实体中的变量都有默认初始化值
                             ***byte,short,int,long -- 0
                             ***float,double -- 0.0
                             ***boolean -- false
                             ***char -- '\u0000' 空字符
                             **实体不在被使用,会在不确定的时间内被垃圾回收器回收

                            C:方法区:后面讲
                            D:本地方法区:window调用系统资源使用。
                            E:寄存器:CPU使用。
                   (5)数组的常见问题
                           A:ArrayIndexOutOfBoundsException
                               访问到了数组中的不存在的索引发生。
                            B:NullPointException
                              arr引用没有指向实体,却在操作实体中的元素时。
                   (6)数组的常见操作
                             A:遍历
                                 数组有个属性:length
                                 使用格式:数组名.length 返回数组的长度(其实就是数组中的元素个数)

                                 public static void printArray(int[] arr)
                                 {
                                  for(int x=0; x<arr.length; x++)
                                  {
                                   System.out.println(arr[x]);
                                  }
                                 }

                              B:获取最值
                                   //获取最大值
                                   public static int getMaxValue(int[] arr)
                                   {
                                    int max = arr[0];

                                    for(int x=1; x<arr.length; x++)
                                    {
                                     if(arr[x]>max)
                                     {
                                      max = arr[x];
                                     }
                                    }

                                    return max;
                                   }

                                   //获取最小值
                                   public static int getMinValue(int[] arr)
                                   {
                                    int min = arr[0];

                                    for(int x=1; x<arr.length; x++)
                                    {
                                     if(arr[x]<min)
                                     {
                                      min = arr[x];
                                     }
                                    }

                                    return min;
                                   }
  
                               C:排序(重点)
                                   (1)选择排序
                                    原理:用0索引位置的元素,和其他的元素进行比较,第一次,最小值出现在了0索引位置。
  
                                    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[y]<arr[x])
                                       {
                                        int temp = arr[x];
                                        arr[x] = arr[y];
                                        arr[y] = temp;
                                       }
                                      }
                                     }
                                    }

                                    (2)冒泡排序
                                     原理:相邻元素两两比较,第一次结束后,最大值出现在了最大索引位置。

                                     public static void bubbleSort(int[] arr)
                                     {
                                      for(int x=0; x<arr.length-1; x++)
                                      {
                                       for(int y=0; y<arr.length-1-x; y++)
                                       {
                                        if(arr[y]>arr[y+1])
                                        {
                                         int temp = arr[y];
                                         arr[y] = arr[y+1];
                                         arr[y+1] = temp;
                                        }
                                       }
                                      }
                                     }

                                 D:查找

                                     1:普通查找
                                      任意数组都可以。

                                      public static int search(int[] arr,int value)
                                      {
                                       int index = -1;

                                       for(int x=0; x<arr.length; x++)
                                       {
                                        if(arr[x]==value)
                                        {
                                         index = x;
                                         break;
                                        }
                                       }
 
                                       return index;
                                      }
 
                                     2:二分查找
                                      前提:数组必须有序。
                                      注意:不能先对数组排序,在进行二分查找。

                                      public static int binarySearch(int[] arr,int value)
                                      {
                                       int min = 0;
                                       int max = arr.length-1;
                                       int mid = (min+max)/2;

                                       while(arr[mid]!=value)
                                       {
                                        if(arr[mid]>value)
                                        {
                                         max = mid - 1;
                                        }
                                        else if(arr[mid]<value)
                                        {
                                         min = mid + 1;
                                        }

                                        mid = (min+max)/2;
                                       }

                                       return mid;
                                      }

                      (7):二维数组
                                  A:数组中的数组。
                                          其实就是一维数组中的每一个元素是一维数组。
                                  B:二维数组的格式
                                         ***int[][] arr = new int[3][2];
                                         ***int[][] arr = new int[3][];
                                         ***int[][] arr = {{12,34,45},{44,67},{98}};
                                  C:二维数组的遍历
                                   public static void printArray2(int[][] arr)
                                   {
                                    for(int x=0; x<arr.length; x++)
                                    {
                                     for(int y=0; y<arr[x].length; y++)
                                     {
                                      System.out.print(arr[x][y]+"  ");
                                     }
                                     System.out.println();
                                    }
                                   }
                                   D:二维数组应用(重点)
                                    二维数组的应用:
                                    一年12个月,每个月的销售额是xx万元.求一年的销售总额.
                                    春季:11 23 34
                                    夏季:45 87 79
                                    秋季:35 23
                                    冬季:25

                                    注意特殊写法情况:
                                  int[] x,y[];
                                  int[] x;
                                  x是一个一维数组
                                  int[] y[];
                                  y是一个二维数组

                   (8)从键盘输入
                                使用步骤:
                                 import java.util.Scanner;
                                 Scanner sc = new Scanner(System.in);
                                 sc.nextInt();获取整型数据
                                 sc.nextLine();获取字符串数据

                   (9)排序方法
                                使用步骤
                                 import java.util.Arrays;

                                 Arrays.sort(arr);

 

                                                                                ------- android培训java培训、期待与您交流! ----------
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值