1 / 6

陣列與排序

陣列與排序. 陣列宣告. 一維陣列 var 陣列名稱 =new Array( 陣列大小 ) ; 範例 : var a=new Array(4) 索引值 由 0 開始,所有 a[0] 、 a[1] 、 a[2] 、 a[3] 、 a[4] 五個元素 指定初值 var 陣列名稱 =new Array( 元素 0, 元素 1 …… ) ; 範例 : var a=new Array(5,4,8,7,1) 省略 new Array 範例 : var a= [ 5,4,8,7,1 ] 注意使用 [] 符號. 陣列處理技巧.

cyma
Télécharger la présentation

陣列與排序

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 陣列與排序 陣列與排序

  2. 陣列宣告 • 一維陣列 var 陣列名稱=new Array(陣列大小) ; 範例: var a=new Array(4) 索引值由0開始,所有a[0]、 a[1]、 a[2]、 a[3]、 a[4]五個元素 • 指定初值 var 陣列名稱=new Array(元素0,元素1……) ; 範例: var a=new Array(5,4,8,7,1) • 省略new Array 範例: var a=[5,4,8,7,1] 注意使用[]符號 陣列與排序

  3. 陣列處理技巧 • 計算陣列長度(元素個數)陣列.length • 將陣列元素排序陣列.sort() • 將陣列轉換為字串,並以“,”分隔每個元素 陣列.join() 陣列與排序

  4. 隨機函數random • 隨機函數Math.random() • 自動產生介於0到1之間的隨機亂數 • 若要產生介於n到m的亂數,公式如下: Math.random()*(m-n+1)+n 範例: x=parseInt(Math.random()*100+1) 產生1 ~100之間的整數亂數指定給x parseInt為轉換為整數的函數 陣列與排序

  5. 氣泡式排序 <SCRIPT language="JavaScript"> document.title="氣泡式排序" var a=[9,3,7,5,1,4] for (i=0;i<a.length;i++) //陣列.length { for (j=i+1;j<6;j++) { if (a[i]<a[j]) { temp=a[i];a[i]=a[j];a[j]=temp; //資料交換 } } document.write("第"+(i+1)+"回比較:"+a+"<br>"); } </SCRIPT> 陣列與排序

  6. Java內建排序函數 <SCRIPT language="JavaScript"> var a=[9,3,7,5,1,4] a.sort(); alert(a); </SCRIPT> 陣列與排序

More Related