java學習2(31-61總結)

來源:互聯網
上載者:User

標籤:

1、for與while相比

for控制迴圈的變數只作用於for迴圈,執行完畢釋放記憶體。比while節省記憶體 2、重載函數名同,參數列表不同與傳回值無關 3、記憶體的劃分:(1)寄存器。(2)本地方法區。(3)方法區。(4)棧記憶體。局部變數(5)堆記憶體。new出來的實體(數組、對象) 4、棧,自動釋放記憶體堆,java記憶體回收機制,不定時。 5、數組的兩種異常ArrayIndexOutOfBoundsException: 運算元組時,訪問到了數組中不存在的下標。NullPointerException: null 指標異常,當引用沒有任何指向,值為null的情況,該引用還在用於操作實體。 6、s.o.p(arr)[[email protected]數組,整數類型,地址為,雜湊值(16進位) 7、選擇排序第一圈,最值出現在第一位
 1 package array; 2 public class ArrayTest { 3  public static void selectSort(int[] arr) 4  { 5   for(int x=0;x 6   { 7    for(int y=x+1;y 8    { 9     if(arr[x]>arr[y])10     {11      int temp=arr[x];12      arr[x]=arr[y];13      arr[y]=temp;14     }15    }16   }17  }18  public static void main(String[] args)19  {20   int []arr={5,1,6,4,2,8,9};21   //排序前22   printArray(arr);23   //排序24   selectSort(arr);25   //排序後26   printArray(arr);27  }28  public static void printArray(int[] arr)29  {30   System.out.print("[");31   for(int x=0;x32   {33    if(x!=arr.length-1)34     System.out.print(arr[x]+", ");35    else36     System.out.print(arr[x]);37   }38   System.out.println("]");39  }40 }

 

 8、冒泡排序

第一圈,最值出現在最後位   
 1 public static void bubbleSort(int[] arr) 2  { 3   for(int x=0;x 4   { 5    for(int y=0;y 6    { 7     if(arr[x]>arr[y]) 8     { 9      int temp=arr[y];10      arr[y]=arr[y+1];11      arr[y]=temp;12     }13    }

 

  9、最快排序希爾排序 PS:真實開發時Arrays.sort(arr); 10、數組中元素交換時,對數組的元素操作,而不是直接對元素操作 11、操作失敗,通常返回-1 12、數組做尋找時,存在多個key值時,擷取key第一次出現在數組中的位置 13、折半尋找前提:數組有序
 1 package array; 2 public class ArrayTest4 { 3  public static void main(String[] args) 4  { 5   int[] arr={2,4,5,7,19,32,45}; 6   int index=halfSearch(arr,32); 7   int index_2=halfSearch_2(arr,2); 8   System.out.println("index="+index); 9   System.out.println("index_2="+index_2);10  }11  public static int halfSearch(int[] arr,int key)12  {13   int min,max,mid;14   min=0;15   max=arr.length-1;16   mid=(min+max)/2;17   while(arr[mid]!=key)18   {19    if(key>arr[mid])20     min=mid+1;21    else if(key22     max=mid-1;23    if(min>max)//如果尋找的值大於最大值,則min無限+1,便大於max24     return -1;25    mid=(max+min)/2;26   }27   return mid;28  }29  public static int halfSearch_2(int[] arr,int key)30  {31   int min=0,max=arr.length-1,mid;32   while(min<=max)33   {34    mid=(max+min)>>1;35   if(key>arr[mid])36    min=mid+1;37   else if(key38    max=mid-1;39   else40    return mid;41   }42   return -1;43  }44  45 }

 

 14、進位轉換(1)十進位-二進位%2/2 (2)十進位-十六進位 >>>4 
 1 package array; 2 public class ArrayTest7 { 3  public static void main(String[] args) 4  { 5   toBin(-6); 6   toBa(15); 7   toHex(60); 8  } 9  public static void toBin(int num)10  {11   //十進位轉-二進位12   trans(num,1,1);13  }14  15  public static void toBa(int num)16  {17   //十進位轉-二進位18   trans(num,7,3);19  }20  21  public static void toHex(int num)22  {23   //十進位轉-十六進位24   trans(num,15,4);25  }26  27  28  public static void trans(int num,int base,int offset)29  {30   if(num==0)31   {32    System.out.println(0);33    return;34   }35   char[] chs={‘0‘,‘1‘,‘2‘,‘3‘,‘4‘,36     ‘5‘,‘6‘,‘7‘,‘8‘,‘9‘,37     ‘A‘,‘B‘,‘C‘,‘D‘,‘E‘,‘F‘};38   char[] arr=new char[32];39   int pos=arr.length;40   while(num!=0)41   {42    int temp=num&base;43    arr[--pos]=chs[temp];44    num=num>>>offset;45   }46   for(int x=pos;x47   {48    System.out.print(arr[x]);49   }50   System.out.println("");51  }52 }

 

 15、查表法建立表格,內部儲存0到F,表格有下標,可調整與進位內部對應 16、字元數組被定義時,空位都是\u0000 17、二維數組(1)初始化int [][]arr=new int[3][4];s.o.p(arr);//[[[email protected]s.o.p(arr[0]);//[[email protected]----------------------------------int[][]arr=new int[3][];s.o.p(arr[0]);//null
(2)定義一維數組:int[] x; int x[];二維數組:int[][]y; int[][]y; int[]y[];

java學習2(31-61總結)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.