Qhov txawv ntawm Arrays thiab Arraylists

Qhov txawv ntawm Arrays thiab Arraylists
Qhov txawv ntawm Arrays thiab Arraylists

Video: Qhov txawv ntawm Arrays thiab Arraylists

Video: Qhov txawv ntawm Arrays thiab Arraylists
Video: PRESTIN Ft. YASMI - Nuj Nplhaib Thiab Ntxawm (Cover) 2024, Kaum ib hlis
Anonim

Arrays vs Arraylists

Arrays yog cov ntaub ntawv siv ntau tshaj plaws los khaws cov khoom sau. Feem ntau cov lus programming muab txoj hauv kev yooj yim tshaj tawm arrays thiab nkag mus rau cov ntsiab lus hauv arrays. Ib qho arraylist tuaj yeem pom tau tias yog dynamic array, uas tuaj yeem loj hlob tuaj. Vim li no, tus programmer tsis tas yuav paub qhov loj ntawm daim ntawv teev npe thaum nws txhais nws.

Dab tsi yog Arrays?

Qhia hauv daim duab 1, yog ib qho ntawm cov lej feem ntau siv los tshaj tawm thiab muab cov txiaj ntsig rau ib qho array. Daim duab 2 qhia txog qhov array yuav zoo li cas hauv lub cim xeeb.

int values[5];

values[0]=100;

values[1]=101;

values[2]=102;

values[3]=103;

values[4]=104;

Daim duab 1: Code rau tshaj tawm thiab muab qhov tseem ceeb rau ib qho array

100 101 102 103 104
Index: 0 1 2 3 4

Daim duab 2: Array khaws cia hauv lub cim xeeb

Saum toj no code, txhais cov array uas tuaj yeem khaws 5 tus lej thiab lawv tau nkag los siv cov lej 0 txog 4. Ib qho khoom tseem ceeb ntawm ib qho array yog tias, tag nrho cov array raug faib ua ib qho thaiv ntawm lub cim xeeb thiab txhua lub ntsiab lus tau txais nws. tus kheej qhov chaw hauv array. Thaum ib qho array raug txhais, nws qhov loj me yog tsau. Yog li yog tias koj tsis paub meej txog qhov luaj li cas ntawm cov array ntawm lub sijhawm sib sau, koj yuav tsum tau txhais cov array loj txaus kom nyob rau sab nyab xeeb. Tab sis, feem ntau ntawm cov sij hawm, peb yeej yuav siv tsawg dua cov ntsiab lus uas peb tau faib. Yog li ntawd, ib tug ntau npaum li cas ntawm lub cim xeeb yog tiag nkim. Ntawm qhov tod tes, yog tias "loj txaus array" tsis loj txaus, qhov kev pab cuam yuav poob.

Dab tsi yog Arraylists?

Ib qho arraylist tuaj yeem pom tau tias yog dynamic array, uas tuaj yeem loj tuaj. Yog li arraylists yog qhov zoo tshaj plaws los siv rau hauv qhov xwm txheej uas koj tsis paub qhov loj ntawm cov ntsiab lus xav tau thaum lub sijhawm tshaj tawm. Hauv Java, arraylists tuaj yeem tuav cov khoom nkaus xwb, lawv tsis tuaj yeem tuav cov hom qub ncaj qha (koj tuaj yeem tso cov hom qub rau hauv ib qho khoom lossis siv cov chav kawm wrapper ntawm hom qub). Feem ntau arraylists tau muab nrog rau txoj hauv kev los ua qhov ntxig, tshem tawm thiab tshawb nrhiav. Lub sij hawm nyuaj ntawm kev nkag mus rau lub ntsiab yog o(1), thaum kev ntxig thiab tshem tawm muaj lub sij hawm nyuaj ntawm o(n). Hauv Java, arraylists tuaj yeem hla dhau los siv foreach loops, iterators lossis tsuas yog siv cov indexes.

Dab tsi yog qhov txawv ntawm Arrays thiab Arraylists

Txawm tias cov arrays thiab arraylists zoo sib xws hauv qhov kev nkag siab tias ob qho tib si siv los khaws cov khoom sau, lawv txawv ntawm lawv txhais li cas. Qhov luaj li cas ntawm cov array yuav tsum tau muab thaum lub array raug txhais, tab sis koj tuaj yeem txhais cov arraylist yam tsis paub qhov loj me. Koj tuaj yeem ntxiv cov ntsiab lus rau ib qho arraylist tom qab nws tau txhais thiab qhov no tsis tuaj yeem nrog arrays. Tab sis hauv Java, arraylists tsis tuaj yeem tuav cov hom qub, tab sis arrays tuaj yeem siv los tuav hom qub. Tab sis yog tias koj xav tau cov qauv ntaub ntawv uas tuaj yeem sib txawv nws qhov loj, arraylist yuav yog qhov kev xaiv zoo tshaj plaws.

Pom zoo: