Cov txheej txheem:

Koj pib pib ArrayList li cas hauv tus tsim hauv Java?
Koj pib pib ArrayList li cas hauv tus tsim hauv Java?

Video: Koj pib pib ArrayList li cas hauv tus tsim hauv Java?

Video: Koj pib pib ArrayList li cas hauv tus tsim hauv Java?
Video: Cas Tsis Xaiv Koj - R-Lin Thoj New Song Original By Zaj Dub [Official MV] 2023 2024, Kaum ib hlis
Anonim

Yog koj xav xwb tshaj tawm nws hauv tus tsim koj tuaj yeem muaj tus lej: ArrayList npe = new ArrayList (); Txwv tsis pub koj ua tau tshaj tawm nws ua teb, thiab ces pib nws hauv tus tsim.

Hais txog qhov no, koj ua li cas pib ArrayList hauv Java?

Hauv qab no yog ntau txoj hauv kev los pib ArrayList hauv Java:

  1. Initialization nrog add() Syntax: ArrayList str = tshiab ArrayList(); str.add("Geeks"); str.add("rau"); str.add("Geeks");
  2. Initialization siv asList()
  3. Initialization siv List.of() method.
  4. Initialization siv lwm Collection.

koj pib ib daim ntawv teev npe li cas? Hauv qab no yog cov hauv qab no los pib ib daim ntawv teev npe:

  1. Siv List.add() method. Txij li cov npe yog ib qho interface, ib tus tsis tuaj yeem ncaj qha instantiate nws.
  2. Siv Arrays. asList()
  3. Siv cov txheej txheem hauv chav kawm Collections. Muaj ntau txoj hauv kev hauv chav Collections uas tuaj yeem siv los ua ib daim ntawv teev npe.
  4. Siv Java 8 kwj.
  5. Siv Java 9 List.

Tom qab ntawd, lo lus nug yog, koj yuav tshaj tawm li cas ArrayList?

Java ArrayList Piv txwv

  1. import java.util.*;
  2. chav ArrayList1{
  3. public static void main(String args){
  4. ArrayList npe = tshiab ArrayList(); // Tsim arraylist.
  5. list.add("Ravi");//Ntxiv cov khoom hauv arraylist.
  6. list.add("Vij");
  7. list.add("Ravi");
  8. list.add("Ajay");

Yuav ua li cas los tsim ib tug constructor nyob rau hauv Java?

Cov cai rau sau Constructor:

  1. Constructor (s) ntawm ib chav kawm yuav tsum muaj tib lub npe raws li cov npe hauv chav kawm uas nws nyob.
  2. Ib tus neeg tsim khoom hauv Java tsis tuaj yeem ua qhov paub daws teeb, kawg, zoo li qub thiab synchronized.
  3. Access modifiers yuav siv tau nyob rau hauv constructor tshaj tawm los tswj nws cov kev nkag tau xws li lwm yam chav kawm yuav hu tau rau tus tsim.

Pom zoo: