Qhov Tseem Ceeb Qhov Sib txawv – Sau vs Teeb
Feem ntau cov lus programming siv arrays khaws cov ntaub ntawv ntawm tib hom. Ib qho teeb meem loj ntawm arrays yog tias, thaum lub array loj tshaj tawm, nws tsis tuaj yeem hloov kho. Yog hais tias tus programmer xav khaws qhov tseem ceeb tshaj qhov array loj, ces nws yuav tsum tsim ib qho tshiab array thiab luam cov ntsiab lus uas twb muaj lawm mus rau qhov tshiab array. Hauv cov xwm txheej no, cov khoom siv tuaj yeem siv tau. Nws muaj peev xwm ntxiv cov ntsiab lus, tshem tawm cov ntsiab lus thiab ntau lwm yam haujlwm nrog kev txhawb nqa ntawm kev sau. Muaj ntau hom kev sau muaj nyob rau hauv cov lus programming xws li Java. Daim ntawv teev npe thiab teeb yog qhov sib cuam tshuam ntawm kev sau hierarchy. Lub hauv paus interface rau lwm yam interfaces yog Sau. Qhov sib txawv tseem ceeb ntawm Daim Ntawv Teev Npe thiab Teeb yog tias Daim Ntawv txhawb nqa khaws cia tib lub caij ntau zaus thaum Teeb tsis txhawb khaws tib lub caij ntau zaus. Yog li ntawd, ib Txheej tsis pub luam tawm.
Dab tsi yog List?
Daim ntawv teev npe yog ib qho kev sib txuas uas txuas ntxiv rau Kev Sau Npe. Muaj ntau txoj hauv kev hauv Collection interface. Txoj kev ntxiv pab ntxiv ib lub caij. Txoj kev 'tshem tawm' yog tshem tawm cov khoom. Muaj 'addAll method' ntxiv ntau lub ntsiab lus thaum 'removeAll method' kom tshem tawm cov ntsiab lus los ntawm kev sau. Cov txheej txheem muaj pab nrhiav seb puas muaj ib yam khoom muaj nyob hauv Daim Ntawv Teev Npe lossis tsis yog. Qhov 'containsAll' yog txhawm rau nrhiav seb puas muaj cov khoom muaj nyob hauv cov khoom sau. Txoj kev iterator yog siv los voj los ntawm cov khoom ntawm cov npe. Raws li Daim Ntawv Tshaj Tawm Tshaj Tawm, txhua txoj hauv kev ntawm Sau yog nyob rau Daim Ntawv Teev Npe. Lwm yam tshaj li cov txheej txheem, Daim Ntawv Teev Npe muaj txoj hauv kev xws li tau txais thiab teeb tsa. Tus programmer tuaj yeem tau txais tus nqi ntawm ib qho kev ntsuas tshwj xeeb uas siv tau txoj kev. Tus programmer tuaj yeem teeb tsa tus nqi ntawm ib qho kev ntsuas tshwj xeeb siv txoj kev teeb tsa. Lub 'indexOf' yog siv los nrhiav qhov ntsuas ntawm ib qho khoom.
Nyob hauv ib Daim Ntawv Teev Npe, cov haujlwm tuaj yeem ua raws li txoj haujlwm. Tus programmer tuaj yeem muab cov ntaub ntawv tseem ceeb uas yuav tsum tau ntxiv rau qhov ntsuas. Yog li nws yuav muab ntxiv rau qhov kev ntsuas tshwj xeeb. Yog tias tus programmer tsis muab qhov ntsuas, lub ntsiab lus yuav raug ntxiv rau qhov kawg ntawm Daim Ntawv Teev Npe. Nws kuj tuav cov kev txiav txim tso. Yog hais tias element 1 ntxiv thiab ces element2 ntxiv, ces element1 yuav ua ntej element2.
Daim duab 01: Sau thiab Teeb
ArrayList, LinkedList, Vector yog qee cov chav kawm uas siv Daim Ntawv Teev Npe. Hauv ArrayList, nkag mus rau ib lub ntsiab lus nrawm tab sis ntxig thiab tshem tawm qis dua. ArrayList tsis muaj xov zoo. Kev nkag mus rau tib ArrayList los ntawm ntau cov xov yuav tsis muab qhov txiaj ntsig zoo ib yam. Hauv LinkedList, cov ntsiab lus txuas rau ob qho tib si rov qab thiab rau pem hauv ntej. Ntxig thiab tshem tawm cov ntsiab lus siv LinkedList nrawm dua li ArrayList. LinkedList siv cov npe thiab Queue Ob leeg. Vector zoo ib yam li ArrayList, tab sis nws yog tread-kev nyab xeeb vim tias txhua txoj kev yog synchronized.
Dab tsi yog Teeb?
Teeb yog ib qho kev sib txuas uas txuas ntxiv rau Kev Sau Npe. Raws li Teeb interface txuas ntxiv Sau, tag nrho cov txheej txheem ntawm Kev Sau kuj yog rau Teeb. Ib Txheej tsis txhawb nqa qhov tseem ceeb duplication. Yog li ntawd, tus programmer tsis tuaj yeem khaws cov khoom qub ob zaug. Nws tuav cov txheej txheem tshwj xeeb. Lub SortedSet interface txuas ntxiv Teem interface. SortedSet tswj cov khoom hauv kev txheeb xyuas. Lub NavigableSet interface txuas ntxiv SortedSet. Lub NavigableSet muab cov kev taw qhia xws li qis, hauv pem teb, qab nthab thiab lwm yam.
HashSet, LinkedHashSet, thiab TreeSet yog qee cov chav kawm uas siv lub teeb interface. HashSet siv Teeb interface. Nws tsis tuav qhov kev txiav txim tso. Yog tias tus nqi tau muab tso rau hauv a, x, b nws yuav khaws cia li, x, a, b. Lub LinkedSet tswj qhov kev txiav txim tso. Yog tias cov ntsiab lus tau muab tso rau hauv a, x, b xaj, qhov kev khaws cia yuav yog a, x, b. TreeSet siv Teeb thiab NavigableSet. Nws tsis tswj qhov kev txiav txim ntawm kev ntxig tab sis khaws cov khoom hauv qhov kev txiav txim. Yog tias qhov kev txiav txim ntxig yog a, c, b, ces cov ntsiab lus yuav muab khaws cia ua a, b, c. Txhua HashSet, LinkedHashSet thiab TreeSet yuav tsis muaj cov ntsiab lus sib npaug.
Dab tsi yog qhov zoo sib xws ntawm Cov Npe thiab Teeb?
- Ob daim ntawv teev npe thiab teeb tsa cuam tshuam txuas ntxiv Sau interface.
- Ob daim ntawv teev npe thiab teeb tsa kev txhawb nqa xws li ntxiv, tshem tawm cov ntsiab lus.
Dab tsi yog Daim Ntawv Teev Npe thiab Teeb?
List vs Set |
|
List Interface yog sub interface ntawm Sau uas muaj cov txheej txheem los ua haujlwm xws li ntxig, tshem tawm raws li qhov ntsuas. | Set Interface yog sub interface ntawm Sau uas muaj cov txheej txheem los ua haujlwm xws li ntxig, tshem tawm cov ntsiab lus thaum tuav cov khoom tshwj xeeb. |
chav kawm | |
ArrayList, Vector, thiab LinkedList yog cov chav kawm uas siv cov npe interface. | HashSet, LinkedHashSet, thiab TreeSet yog chav kawm uas siv Teeb interface. |
Yam Duplication | |
Lus txhawb kev sib tw ntawm cov ntsiab lus. | Set tsis txhawb nqa cov khoom sib tw. Cov ntsiab lus yog qhov tshwj xeeb. |
Summary – List vs Set
Cov khoom siv yog siv los khaws cov ntsiab lus dynamically. Programming lus xws li Java muab Collection interface. Daim ntawv teev npe thiab teeb yog ob qhov sib cuam tshuam uas yog rau Sau interface. Ob qho tib si interfaces txuas ntxiv Sau. Kab lus no tau tham txog qhov sib txawv ntawm Daim Ntawv Teev Npe thiab Teeb. Qhov sib txawv tseem ceeb ntawm Daim Ntawv Teev Npe thiab Teeb yog tias Daim Ntawv txhawb nqa khaws cia tib lub caij ntau zaus thaum Teeb tsis txhawb nqa tib lub caij ntau lub sijhawm. Teem ib txwm tuav cov khoom tshwj xeeb.