用陣列做引數的例子

2021-04-20 01:49:58 字數 2220 閱讀 6455

dim maxbound   

maxbound    =    ubound(arr)   

redim    preserve arr(    maxbound    + 1)   

arr(maxbound    +    1)    = newitem   

end sub   

sub remove( byref    arr(),olditem)   

』用於移出陣列舊內容的過程   

dim maxbound,old,temparr(),i     

maxbound    =    ubound(arr)   

exits    =    false   

for    each    old    in    arr   

if    old    =    olditem    then    maxbound    =    maxbound    -1   

next   

』如果不存在那麼   

if    maxbound    =    ubound(arr)    then    exit    sub   

redim    temparr(maxbound)   

i    =    0   

for    each    old    in    arr   

if    old    <>    olditem    then   

temparr(i)    =    old   

i    =    i    +    1   

end    if   

next   

redim    arr(    maxbound    )   

for    i    =    0    to    maxbound   

arr(i)      =    temparr(i)     

next   

end sub   

sub    sort(byref    arr())   

』氣泡排序   

dim    loop1   

dim    loop2   

dim    temp   

for    loop1    =    ubound(arr)    to    0    step    -1   

for    loop2    =        1    to    loop1   

if    arr(loop2    -    1)    >    arr(loop2)    then   

temp    =    arr(loop2    -    1)   

arr(loop2    -    1)    =    arr(loop2)   

arr(loop2)    =    temp   

end    if   

next       

next       

end sub   

』下面是用來測試執行前後結果變化的   

aa    =    array("gg兔","mm兔","bt兔")   

document.write    "顯示原始陣列"    &    "

"   

show    aa   

document.write    "新增一條並顯示"    &    "

"   

show    aa   

document.write    "新增一條並顯示"    &    "

"   

show    aa   

document.write    "排序並顯示"    &    "

"   

sort    aa   

show    aa   

document.write    "刪除一條並顯示"    &    "

"   

remove    aa,"狼皮兔"   

show    aa   

sub show(arr)   

』顯示陣列的全部內容   

for each    a    in    arr   

document.write    a    &    "

"   

next   

document.write    a    &    "

"   

end sub

陣列做函式引數

陣列名是陣列元素首位址,可以直接當做實參,可以用指標或方式來當形參接收,都是位址傳遞,形參都是指標方式 include void input int arr void output int arr 這裡得出的大小是4個位元組,證明形參是指標形式 printf n d sizeof arr void ...

用指標做函式引數的好處,

用指標做函式引數的好處,首先要理解函式傳參的過程,函式傳參是複製型的,例如 void modify int a a void main int a 5 modify a printf d a 程式執行完之後,a的值還是5,為什麼呢,因為在執行modify函式的時候,是另外開闢了儲存空間,將a的值複製...

Switch能否用string做引數

在jdk 7 之前,switch 只能支援 byte short char int 這幾個基本資料型別和其對應的封裝型別。switch後面的括號裡面只能放int型別的值,但由於byte,short,char型別,它們會 自動 轉換為int型別 精精度小的向大的轉化 所以它們也支援。注意,對於精度比i...