Peb puas tuaj yeem siv tus piv nrog ArrayList?
Peb puas tuaj yeem siv tus piv nrog ArrayList?

Video: Peb puas tuaj yeem siv tus piv nrog ArrayList?

Video: Peb puas tuaj yeem siv tus piv nrog ArrayList?
Video: Puas Kam Pob Tuaj Nrog Nej Nyeb - Koos Loos 2024, Hlis ntuj nqeg
Anonim

Hauv luv luv, los txheeb ib qho ArrayList siv a Muab piv koj yuav tsum: Tsim ib qho tshiab ArrayList . Populate cov arrayList nrog cov ntsiab lus, siv ntxiv (E e) API txoj kev ntawm ArrayList . Invoke reverseOrder() API txoj kev ntawm Collections kom tau txais a Muab piv uas imposes qhov thim rov qab ntawm lub ntuj txiav txim ntawm daim ntawv teev cov ntsiab lus.

Ib yam li ntawd, ArrayList puas siv piv tau?

sort() txoj kev xaiv ArrayList cov ntsiab lus lossis cov ntsiab lus ntawm lwm daim ntawv teev npe kev siv muab cov ntsiab lus yog piv . Qhov no txhais li cas programmatically yog tias cov chav kawm ntawm cov ntsiab lus xav tau siv tus Sib piv interface ntawm lub java ua . lang pob.

Ib tug kuj yuav nug, vim li cas peb thiaj li siv qhov sib piv hauv Java? Sib piv v/s Sib piv hauv Java Comparable interface yog siv txhawm rau txheeb cov khoom nrog natural ordering. Sib piv hauv Java yog siv los txheeb cov cwj pwm ntawm cov khoom sib txawv. Comparable interface piv "qhov no" siv nrog cov khoom teev. Sib piv hauv Java muab piv rau ob yam khoom sib txawv hauv chav kawm.

Tsis tas li ntawd, qhov sib piv thiab qhov sib piv ua haujlwm li cas hauv?

Nrog Sib piv , koj chav kawm yuav tsum tau siv cov piv interface thiab koj yuav tsum override nws txoj kev sib piv. Yog li thaum koj hu rau Collections. xaiv txoj hauv kev ntawm ib yam khoom ntawm koj chav kawm, koj qhov kev siv ntawm txoj kev piv txwv hu ua Sab hauv , thiab cov khoom raug txheeb raws li.

Koj tuaj yeem txheeb ArrayList hauv Java?

Txoj Kev: Ib ArrayList tuaj yeem ua Xam los ntawm kev siv lub xaiv () method of the Collections Class in Java . Qhov no xaiv () txoj kev yuav siv lub sau los ua txheeb raws li qhov parameter thiab xa rov qab sau txheeb nyob rau hauv Ascending Order los ntawm lub neej ntawd.

Pom zoo: