Cov txheej txheem:

Yuav ua li cas koj tsim ib vector hauv Java?
Yuav ua li cas koj tsim ib vector hauv Java?

Video: Yuav ua li cas koj tsim ib vector hauv Java?

Video: Yuav ua li cas koj tsim ib vector hauv Java?
Video: Thaum muaj txoj kev nyuaj siab yuav ua li cas...? _ LIS XYOOJ muab lus qhia. 2024, Kaum ib hlis
Anonim

Rau tsim a vector , siv peb kauj ruam: Tshaj tawm qhov sib txawv los tuav lub vector . Tshaj tawm ib qho tshiab vector object thiab muab nws rau lub vector sib txawv. Khaws cov khoom hauv vector , piv txwv li, nrog rau addElement method.

Tsis tas li cov lus nug yog, koj ua li cas tsim cov khoom vector hauv Java?

Piv txwv 1

  1. import java.util.*;
  2. public class VectorExample1 {
  3. public static void main(String args) {
  4. // Tsim ib qho vector khoob uas muaj peev xwm pib 4.
  5. Vector vec = new Vector(4);
  6. // Ntxiv cov ntsiab lus rau vector.
  7. vec.add("Tig");
  8. vec.add("liab");

Ib sab saum toj no, koj yuav ua li cas tsim vector array hauv Java? Tau qhov Vector . Hloov ua lwm yam Vector rau Object array siv toArray() method. Hloov cov khoom array rau yam xav tau array siv Arrays . copyOf() txoj kev.

Txoj kev:

  1. Tsim ib hom Vector String.
  2. Ntxiv cov khoom rau hauv Vector siv add(E) method.
  3. Hloov Vector rau Array siv toArray(New String[vector. size()]).

Yog li ntawd, dab tsi yog vector hauv Java?

Cov java ua .util. Vector chav kawm siv cov khoom loj hlob tuaj. Zoo ib yam li Array, nws muaj cov khoom siv uas tuaj yeem nkag tau siv qhov ntsuas qhov sib npaug. Hauv qab no yog cov ntsiab lus tseem ceeb txog Vector - Qhov loj me ntawm a Vector tuaj yeem loj hlob lossis ntsws raws li xav tau kom haum rau kev ntxiv thiab tshem tawm cov khoom.

Vim li cas vector tsis siv hauv Java?

Vector chav kawm ua ke ob yam ntxwv - "Re-sizable Array" thiab "Synchronization". Qhov no ua rau tsis zoo tsim. Vim tias, yog tias koj xav tau tsuas yog "Re-sizable Array" thiab koj siv Vector chav kawm rau qhov ntawd, koj yuav tau txais "synchronized Resizable Array" tsis yog tsuas yog re-sizable array. Qhov no tuaj yeem txo qhov kev ua haujlwm ntawm koj daim ntawv thov.

Pom zoo: