java學習第05天(數組常見操作、數組中的數組)

來源:互聯網
上載者:User

標籤:char   數字   第一個   axel   寫法   對應關係   需要   冒泡   方式   

(4)數組常見操作

a.遍曆取值

class  ArrayDemo3{   public static void main(String[] args)   {      //System.out.println("Hello World!");      //格式1      /*      需要一個容器,但不明確容器的具體資料。      */      //int[] arr = new int[8];         //格式2      /*      需要一個容器,儲存一直的具體資料。      */      //元素類型[] 數組名 = new 元素類型[](元素,元素,....);      //int[] arr = new int[]{12,25,58,69};      int[] arr = {12,25,58,69};//寫法相對較簡單      /*      對數組的操作最基本的功能就是存和取。      核心思想:就是對角標的操作。      */      //System.out.println(arr[0]);      //System.out.println(arr[1]);      //System.out.println(arr[2]);      //System.out.println(arr[3]);      //System.out.println(arr.length);//arr數組的長度      for(int x=0;x<arr.length ;x++)      {           
      System.out.println("arr["+x+"]="+arr[x]+";"); } }}

 

b.擷取最值(最大值,最小值)

class ArrayDemo4{   public static void main(String[] args)   {      int[] arr = {25,56,35,97,39};      int max = getMax(arr);      System.out.println("max="+max);   }    /*   擷取數組中的最大值   1.需要進行比較,並定義變數記錄每次比較後較大的值。   2.對數組中的元素進行遍曆取出,和變數中記錄的元素進行比較。      如果遍曆到的元素大於變數中記錄的元素,就用變數記錄住較大的值。   3.遍曆結束,該變數記錄就是最大值。    定義一個功能來實現。   明確一,結果        是數組中的元素。   明確二,位置內容        數組。   */   public static int getMax(int[] arr)   {      //定義變數記錄較大的值。      int maxElement = arr[0];      for(int x = 0;x<arr.length ;x++)      {        if(arr[x]>maxElement)        {           maxElement = arr[x];        }      }      return maxElement;   }    public static int getMax_2(int[] arr)   {      //定義變數記錄較大的值。      int maxIndex = 0;      for(int x = 0;x<arr.length ;x++)      {        if(arr[x]>arr[maxIndex])        {           maxIndex = x;        }      }      return arr[maxIndex];   }}

 

c.排序(選擇排序、冒泡排序)

class ArrayDemo4{   public static void main(String[] args)   {      int[] arr = {25,56,35,97,39};          selectSort(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])           {              int temp = arr[x];              arr[x] = arr[y];              arr[y] = temp;           }              }      }   }   /*   冒泡排序   */   public static void bubbleSort(int[] arr)   {      for(int x=0;x<arr.length;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.拆半尋找(二分尋找)

(5)數組中的數組

class ArrayDemo5{   public static void main(String[] args)   {      //int [] arr = {5,8,12,54,23,69,52};      //int index = getIndex(arr,619);      //int [] arr = {5,8,12,23,39,69,95};      //int index = halfSearch_2(arr,70);      //System.out.println(index);      //int index1 = Arrays.binarySearch(arr,12);//如果存在,返回的是具體角標位,如果不存在,返回的是 -插入點-1;      //System.out.println("index1="+index1);       toHex_1(60);      //System.out.println("index1="+index1);   }    /*   二分尋找法   */   public static int halfSearch(int[] arr,int key)   {      int max,min,mid;      min = 0;      max = arr.length-1;      mid = (min+max)/2;      while(arr[mid] != key)      {        if(key>arr[mid])        {           min = mid+1;        }else        {           max = mid-1;        }        if(max<min)        {           return -1;        }        mid = (min+max)/2;      }      return mid;   }    public static int halfSearch_2(int[] arr, int key)   {      int max,min,mid;      min = 0;      max = arr.length-1;      while(min<max)      {        mid = (min+max)>>1;//右移1位就是除2.        if(key>arr[mid])           min = mid+1;        else if(key<arr[mid])           max = mid-1;        else           return mid;      }      return -1;   }   /*   數組常見功能:尋找。如果數組中有兩個目的元素,返回的是第一個元素所在的索引。   */   /*public static int getIndex(int arr[],int key)   {      for(int x=0;x<arr.length;x++)      {        if(arr[x] == key)        {           return x;        }      }      return -1;   }*/    /*   什麼時候使用數組呢?   如果資料出現了對應關係,而且對應關係的一方是有序的數字編號,並作為角標使用,   這時候就必須要想到數組的使用。    就可以將這些資料儲存到數組中。   根據運算的結果作為角標直接去查數組中對應的元素即可。    這種方式:稱為查表法。   */   public static void toHex_1(int num){      //定義一個對應關係表      char[] chs = {‘1‘,‘2‘,‘3‘,‘4‘,‘5‘,‘6‘,‘7‘,‘8‘,‘9‘,‘A‘,‘B‘,‘C‘,‘D‘,‘E‘,‘F‘};      for(int x=0;x<8;x++)      {        int temp = num & 15;        System.out.print(chs[temp]);        num = num >>>4;      }   }    public static void toHex_2(int num){      //定義一個對應關係表      char[] chs = {‘1‘,‘2‘,‘3‘,‘4‘,‘5‘,‘6‘,‘7‘,‘8‘,‘9‘,‘A‘,‘B‘,‘C‘,‘D‘,‘E‘,‘F‘};      /*      一會查表會查到比較多的資料      資料一多,就先存起來,再進行操作。      所以定義一個數組,臨時容器      */      char[] arr = new char[8];      int pos = 0;      while(num != 0)      {        int tamp = num&15;        arr[pos++] = chs[temp];        num = num >>>4;      }   } }

 

java學習第05天(數組常見操作、數組中的數組)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.