Arraylist vs Vector
Ib qho arraylist tuaj yeem pom tau tias yog dynamic array, uas tuaj yeem loj tuaj. Vim li no, tus programmer tsis tas yuav paub qhov luaj li cas ntawm cov arraylist thaum nws txhais nws. Vector kuj pom tau tias yog ib qho array uas tuaj yeem loj hlob tuaj. Vectors tuaj yeem faib tau yooj yim thiab siv tau rau thaum qhov xav tau qhov loj me ntawm qhov chaw cia tsis paub txog thaum lub sijhawm ua haujlwm.
Dab tsi yog Arraylist?
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. Hauv Java, arraylists tau qhia los ntawm version 1.2 thiab nws yog ib feem ntawm Java Collections Framework.
Vector yog dab tsi?
Vector kuj yog ib qho array uas tuaj yeem loj tuaj. Vectors tuaj yeem faib tau yooj yim thiab tuaj yeem siv tau thaum qhov xav tau qhov loj me ntawm qhov chaw cia tsis paub txog thaum lub sijhawm ua haujlwm. Vectors tseem tuaj yeem tuav cov khoom thiab tsis tuaj yeem tuav cov hom qub. Vectors yog synchronized, yog li siv tau zoo nyob rau hauv ib puag ncig multithreaded. Vectors tau muab txoj hauv kev ntxiv cov khoom, tshem tawm cov khoom thiab tshawb nrhiav cov khoom. Zoo ib yam li arraylist hauv java, vectors tuaj yeem hla siv foreach loops, iterators lossis tsuas yog siv cov indexes. Thaum nws los txog rau Java, vectors tau suav txij li thawj version ntawm Java.
Qhov txawv ntawm Arraylist thiab Vector yog dab tsi?
Txawm hais tias ob qho tib si arraylists thiab vectors zoo ib yam li cov dynamic arrays uas tuaj yeem loj hlob tuaj, lawv muaj qee qhov sib txawv tseem ceeb. Qhov sib txawv tseem ceeb ntawm cov arraylists thiab vectors yog tias cov vectors tau synchronized thaum cov arraylists tsis ua haujlwm. Yog li ntawd, siv arraylists nyob rau hauv multithreaded ib puag ncig yuav tsis haum, thaum vectors yuav siv tau yam xyuam xim nyob rau hauv multithreaded ib puag ncig (vim lawv yog xov zoo). Tab sis synchronization hauv vectors yuav ua rau txo qis kev ua haujlwm. Yog li nws yuav tsis yog ib lub tswv yim zoo los siv vectors hauv ib puag ncig threaded. Sab hauv, ob qho tib si arraylists thiab vectors siv arrays los tuav cov khoom. Thaum qhov chaw tam sim no tsis txaus, vectors yuav muab ob npaug rau qhov loj ntawm nws cov array sab hauv, thaum arraylists nce qhov loj ntawm nws cov array sab hauv los ntawm 50%. Tab sis thaum siv ob qho tib si arraylists thiab vectors, los ntawm kev muab lub peev xwm pib tsim nyog, qhov tsis tsim nyog resizing ntawm cov array sab hauv tuaj yeem zam tau. Nyob rau hauv qhov xwm txheej uas qhov kev loj hlob ntawm cov ntaub ntawv paub, siv vectors yuav tsim nyog dua vim tias qhov nce ntxiv ntawm vectors tuaj yeem txhais tau.