Qhov Tseem Ceeb - ArrayList vs LinkedList
Cov ntawv sau muaj txiaj ntsig rau khaws cov ntaub ntawv. Nyob rau hauv ib txwm array, array loj yog tsau. Qee zaum nws yuav tsum tsim cov arrays uas tuaj yeem loj hlob raws li qhov xav tau. Programming lus xws li Java muaj cov sau. Nws yog ib lub moj khaum nrog ib txheej ntawm cov chav kawm thiab interfaces. Nws ua haujlwm raws li lub thawv rau ib pawg ntawm cov ntsiab lus. Collections tso cai rau khaws, hloov tshiab, retrieve cov ntsiab lus. Nws pab ua haujlwm nrog cov ntaub ntawv tsim xws li cov npe, teeb tsa, ntoo thiab daim duab qhia chaw. Daim ntawv teev npe yog ib qho interface ntawm Collection moj khaum. ArrayList thiab LinkedList yog ob chav kawm hauv cov txheej txheem sau. Lawv siv cov khoom sib sau ua ke thiab List interface. Kab lus no tham txog qhov sib txawv ntawm ArrayList thiab LinkedList. ArrayList yog ib chav kawm uas txuas ntxiv rau AbstractList thiab siv cov List interface, uas siv cov dynamic array los khaws cov ntaub ntawv. LinkedList yog ib chav kawm uas txuas ntxiv rau AbstractSequentialList thiab siv Cov Ntawv Teev Npe, Deque, thiab Queue interfaces, uas siv cov npe txuas ob npaug los khaws cov ntaub ntawv. Qhov ntawd yog qhov sib txawv tseem ceeb ntawm ArrayList thiab LinkedList.
ArrayList yog dab tsi?
Cov chav ArrayList yog siv los tsim cov dynamic arrays. Tsis zoo li ib txwm array, qhov loj ntawm dynamic array tsis tas. Ib yam khoom tsim siv ArrayList chav kawm tau tso cai khaws cov khoom hauv cov npe. Lub peev xwm nce siab, yog li tus programmer tuaj yeem ntxiv cov ntsiab lus rau cov npe. ArrayList chav kawm txuas ntxiv cov chav kawm AbstractList uas siv cov npe interface. Yog li, cov txheej txheem ntawm List interface tuaj yeem siv los ntawm ArrayList. Txhawm rau nkag mus rau cov ntsiab lus, txoj kev get() yog siv. Txoj kev add() tuaj yeem siv los ntxiv cov ntsiab lus rau cov npe. Txoj kev tshem tawm yog siv los tshem tawm cov khoom tawm ntawm daim ntawv. Xa mus rau qhov program hauv qab no.
Daim duab 01: Piv txwv ntawm ArrayList
Raws li cov kev pab cuam saum toj no, ib qho khoom ntawm ArrayList yog tsim. Siv cov txheej txheem ntxiv, cov ntsiab lus tuaj yeem ntxiv dynamically. Cov ntsiab lus "A", "B", "C", "D" thiab "E" raug ntxiv siv txoj kev ntxiv. Txoj kev tshem tawm yog siv los tshem tawm ib qho ntawm cov npe. Thaum dhau 4 mus rau txoj kev tshem tawm, tsab ntawv nyob rau hauv 4th Performance index uas yog "E" raug tshem tawm ntawm daim ntawv. Thaum iterating los ntawm cov npe siv lub voj, cov ntawv A, B, C thiab D yuav luam tawm.
LinkedList yog dab tsi?
Zoo ib yam li ArrayList, LinkedList yog siv los khaws cov ntaub ntawv cov ntsiab lus dynamically. Ib yam khoom tsim siv LinkedList chav kawm tau tso cai khaws cov khoom hauv cov npe. Lub peev xwm nce siab, yog li tus programmer tuaj yeem ntxiv cov ntsiab lus rau cov npe. Nws sab hauv siv ob npaug txuas cov npe los khaws cov ntaub ntawv. Hauv ib daim ntawv txuas ob npaug, cov ntaub ntawv khaws cia ua cov nodes. Txhua node muaj ob qhov txuas. Thawj qhov txuas taw qhia mus rau ntawm qhov dhau los. Qhov txuas tom ntej no taw qhia rau cov kab tom ntej hauv kab ke.
Cov chav kawm LinkedList txuas ntxiv cov chav kawm AbstractSequentialList thiab siv cov List interface. Yog li, cov txheej txheem ntawm List interface tuaj yeem siv los ntawm LinkedList. Txoj kev get() tuaj yeem siv los nkag mus rau cov npe ntawm cov npe. Txoj kev add() tuaj yeem siv los ntxiv cov ntsiab lus rau cov npe. Txoj kev tshem tawm yog siv los tshem tawm cov khoom tawm ntawm daim ntawv. Xa mus rau qhov program hauv qab no.
Daim duab 02: Piv txwv nrog LinkedList
Raws li cov kev pab cuam saum toj no, ib qho khoom ntawm LinkedList yog tsim. Siv cov txheej txheem ntxiv, cov ntsiab lus tuaj yeem ntxiv dynamically. Cov ntsiab lus "A", "B", "C", "D" thiab "E" raug ntxiv siv txoj kev ntxiv. Txoj kev tshem tawm yog siv los tshem tawm ib qho ntawm cov npe. Thaum dhau 4 mus rau txoj kev tshem tawm, tsab ntawv nyob rau hauv 4th Performance index uas yog "E" tshem tawm ntawm daim ntawv. Thaum iterating siv lub voj, cov ntawv A, B, C thiab D yuav luam tawm.
Dab tsi zoo sib xws ntawm ArrayList thiab LinkedList?
- Ob leeg ArrayList thiab LinkedList siv cov npe interface.
- Ob qho ArrayList thiab LinkedList tuaj yeem muaj cov khoom sib npaug.
- Ob leeg ArrayList thiab LinkedList tuav qhov kev txiav txim tso.
Qhov txawv ntawm ArrayList thiab LinkedList yog dab tsi?
ArrayList vs LinkedList |
|
ArrayList yog ib chav kawm uas txuas ntxiv rau AbstractList thiab siv cov npe sib cuam tshuam uas siv sab hauv siv cov dynamic array los khaws cov ntaub ntawv. | LinkedList yog ib chav kawm uas txuas ntxiv AbstractSequentialList thiab siv cov npe, Deque, Queue interfaces, uas siv cov npe txuas ob npaug los khaws cov ntaub ntawv. |
Cov ntsiab lus nkag | |
Nkag mus rau cov ntsiab lus ntawm ArrayList sai dua ntawm LinkedList. | Nkag mus rau cov ntsiab lus ntawm LinkedList qeeb dua ntawm ArrayList. |
Kev tswj hwm | |
Kev tswj cov ntsiab lus ntawm ArrayList qeeb dua ntawm LinkedList. | Kev tswj cov ntsiab lus ntawm LinkedList sai dua ntawm ArrayList. |
Kev coj cwj pwm | |
ArrayList ua tau raws li Daim Ntawv Teev Npe. | LinkedList ua tau raws li Daim Ntawv Teev Npe thiab Queue. |
Summary – ArrayList vs LinkedList
Cov txheej txheem sau cia tso cai ua haujlwm nrog cov ntaub ntawv tsim xws li cov npe, ntoo, daim duab qhia chaw thiab teeb tsa. Daim ntawv teev npe yog ib qho kev sib txuas ntawm lub moj khaum sau. Kab lus no tau tham txog qhov sib txawv ntawm ArrayList thiab LinkedList. ArrayList yog ib chav kawm uas txuas ntxiv AbstractList thiab siv cov List interface uas siv cov dynamic array los khaws cov ntaub ntawv. LinkedList yog ib chav kawm uas txuas ntxiv rau AbstractSequentialList thiab siv Daim Ntawv Teev Npe, Deque, Queue interfaces, uas siv cov npe txuas ob npaug los khaws cov ntaub ntawv. Qhov ntawd yog qhov txawv ntawm ArrayList thiab LinkedList.