Cov txheej txheem:

Koj yuav ua li cas tshem tawm qhov tseem ceeb ntawm qhov ArrayList?
Koj yuav ua li cas tshem tawm qhov tseem ceeb ntawm qhov ArrayList?

Video: Koj yuav ua li cas tshem tawm qhov tseem ceeb ntawm qhov ArrayList?

Video: Koj yuav ua li cas tshem tawm qhov tseem ceeb ntawm qhov ArrayList?
Video: QHIA QHOV TSEEB - KUB VAJ LOOG TSUA NEW MUSIC VIDEO 2022 2024, Kaum ib hlis
Anonim

Rau tshem tawm tus duplicates los ntawm arraylist , peb tuaj yeem siv java 8 kwj api ib yam. Siv chav qhov txawv () txoj kev uas xa rov qab cov kwj uas muaj qhov sib txawv cov ntsiab lus muab piv los ntawm object's equals() method. Sau tag nrho cheeb tsam cov ntsiab lus raws li Daim Ntawv Teev Npe siv Cov Sau. rauList().

Tom qab ntawd, ib tus tuaj yeem nug, koj yuav ua li cas tshem tawm cov neeg ua haujlwm sib npaug ntawm ArrayList hauv Java?

Txoj kev:

  1. Tau txais ArrayList nrog cov nqi sib npaug.
  2. Tsim lwm ArrayList.
  3. Hla mus los ntawm thawj arraylist thiab khaws thawj qhov tshwm sim ntawm txhua lub hauv paus rau hauv daim ntawv thib ob uas siv muaj () txoj kev.
  4. Qhov thib ob ArrayList muaj cov ntsiab lus nrog duplicates tshem tawm.

Ib tug kuj yuav nug, yuav ua li cas koj tshem tawm duplicate nodes nyob rau hauv daim ntawv txuas? Tshem cov duplicates los ntawm cov npe sib txuas

  1. Algorithm: Hla cov npe los ntawm lub taub hau (los yog pib) ntawm. Thaum traversing, sib piv txhua qhov nrog nws cov tom ntej node. Yog tias cov ntaub ntawv ntawm cov node tom ntej no zoo ib yam li cov node tam sim no ces rho tawm cov tom ntej node.
  2. Kev nqis tes ua: Cov haujlwm uas tsis yog tshem tawm Duplicates() tsuas yog los tsim cov npe txuas txuas thiab sim tshem tawmDuplicates().

Yog li ntawd, koj yuav ua li cas tshem tawm duplicates hauv Java?

Peb ua tau tshem tawm duplicate element nyob rau hauv ib qho array los ntawm 2 txoj kev: siv ib ntus array los yog siv cais index. Rau tshem tawm tus luam tawm lub ntsiab los ntawm array, lub array yuav tsum nyob rau hauv kev txiav txim. Yog tias array tsis raug txheeb xyuas, koj tuaj yeem txheeb nws los ntawm kev hu rau Arrays. sort(arr) txoj kev.

Peb puas tuaj yeem ntxiv cov nqi sib npaug hauv ArrayList?

ArrayList arraylist = nov ArrayList (); //Txoj hlua tus nqi yog khaws cia rau hauv array lis arraylist . ntxiv ("Tsim txoj hlua"); 4) Duplicates : ArrayList tso cai duplicate cov ntsiab lus tab sis HashMap tsis tso cai luam tawm yuam sij (Nws tso cai duplicate nqi ). 5) Tsis muaj: ArrayList tuaj yeem muaj tus naj npawb ntawm null cov ntsiab lus.

Pom zoo: