Iindlela zeJava ArrayList ezinemizekelo

Kule posi sigubungela ezona ndlela zixhaphakileyo zeJava ArrayList ngemizekelo yekhowudi yendlela nganye.

I ArrayList iklasi luluhlu olunokuphinda lube nakho. Sihlala sisebenzisa ArrayList xa singabazi ubungakanani boludwe kwangaphambili.

I ArrayList iklasi ikwi java.util iphakheji.




Uyenza njani iArrayList

Ukwenza i-ArrayList kunye nokongeza izinto kuyo, siqinisekisa ArrayList into enoluhlobo oluchaziweyo, njenge String okanye Integer.

Qaphela i add() indlela yongeza into kwindawo elandelayo kwi-ArrayList. Jonga umzekelo olandelayo ukubona ukuba ungayongeza njani into kwisalathiso esithile.


import java.util.ArrayList; public class ArrayListTutorial { public static void main(String[] args) {
ArrayList fruitBasket = new ArrayList();
fruitBasket.add('Apple');
fruitBasket.add('Banana');
fruitBasket.add('Grapes');
fruitBasket.add('Orange');
System.out.println(fruitBasket); } }

Iziphumo:

[Apple, Banana, Grapes, Orange]

Ungayongeza njani into kwindawo ethile

Ukongeza into kwindawo echaziweyo, kufuneka sinikeze isalathiso esifunwayo kwi | _ _ _ _ | indlela.

add()

Iziphumo:

import java.util.ArrayList; public class ArrayListTutorial { public static void main(String[] args) {
ArrayList fruitBasket = new ArrayList();
fruitBasket.add('Apple');
fruitBasket.add('Banana');
fruitBasket.add('Grapes');
fruitBasket.add('Orange');
fruitBasket.add(2, 'Melon');
System.out.println(fruitBasket); } }


Uyifumana njani into kuLuhlu lweArray

I [Apple, Banana, Melon, Grapes, Orange] Indlela yokubuyisa into. Kuya kufuneka sinikeze inombolo yesalathiso kwi get() | indlela.


get()import java.util.ArrayList; public class ArrayListTutorial { public static void main(String[] args) {
ArrayList fruitBasket = new ArrayList();
fruitBasket.add('Apple');
fruitBasket.add('Banana');
fruitBasket.add('Grapes');
fruitBasket.add('Orange');
System.out.println(fruitBasket.get(1)); } }


Uyisusa njani into kuLuhlu lweArray

Ukususa into, sisebenzisa Banana indlela. Kuya kufuneka sinikeze inombolo yesalathiso.

remove()

Iziphumo:

import java.util.ArrayList; public class ArrayListTutorial { public static void main(String[] args) {
ArrayList fruitBasket = new ArrayList();
fruitBasket.add('Apple');
fruitBasket.add('Banana');
fruitBasket.add('Grapes');
fruitBasket.add('Orange');
fruitBasket.remove(0);
System.out.println(fruitBasket); } }


Ungazisusa njani zonke izinto kuLuhlu lweArray

I [Banana, Grapes, Orange] indlela isusa zonke izinto kwi | _ _ _ _ |.

clear()ArrayList

Ufumana njani ubungakanani beArrayList

import java.util.ArrayList; public class ArrayListTutorial { public static void main(String[] args) {
ArrayList fruitBasket = new ArrayList();
fruitBasket.add('Apple');
fruitBasket.add('Banana');
fruitBasket.add('Grapes');
fruitBasket.add('Orange');
fruitBasket.clear();
System.out.println(fruitBasket); } }

Iziphumo:


[]

Uyikhupha njani iArrayList

import java.util.ArrayList; public class ArrayListTutorial { public static void main(String[] args) {
ArrayList fruitBasket = new ArrayList();
fruitBasket.add('Apple');
fruitBasket.add('Banana');
fruitBasket.add('Grapes');
fruitBasket.add('Orange');
System.out.println(fruitBasket.size()); } }

Iziphumo:

4

Ukutyibilika kuLuhlu lweLuhlu nge-for nganye

import java.util.ArrayList; public class ArrayListTutorial { public static void main(String[] args) {
ArrayList fruitBasket = new ArrayList();
fruitBasket.add('Apple');
fruitBasket.add('Banana');
fruitBasket.add('Grapes');
fruitBasket.add('Orange');
for (int i = 0; i < fruitBasket.size(); i++) {
System.out.println(fruitBasket.get(i));
} } }

Iziphumo:

Apple Banana Grapes Orange

Uluhlu lweArrayList ngamanani

import java.util.ArrayList; public class ArrayListTutorial { public static void main(String[] args) {
ArrayList fruitBasket = new ArrayList();
fruitBasket.add('Apple');
fruitBasket.add('Banana');
fruitBasket.add('Grapes');
fruitBasket.add('Orange');
for (String i : fruitBasket) {
System.out.println(i);
} } }

Iziphumo:

Apple Banana Grapes Orange

Ungawutshintsha njani umxholo kuLuhlu lweArray

I import java.util.ArrayList; public class ArrayListTutorial { public static void main(String[] args) {
ArrayList numbers = new ArrayList();
numbers.add(10);
numbers.add(20);
numbers.add(30);
numbers.add(40);
for (int i : numbers) {
System.out.println(i);
} } }
indlela ithatha indawo yento yenye into ebonelelweyo. Kuya kufuneka sichaze isalathiso sento esifuna ukuyibuyisela.


10 20 30 40

Iziphumo:

replace()

Ungazitshintsha njani izinto kuLuhlu lweeArray

I import java.util.ArrayList; public class ArrayListTutorial { public static void main(String[] args) {
ArrayList fruitBasket = new ArrayList();
fruitBasket.add('Apple');
fruitBasket.add('Banana');
fruitBasket.add('Grapes');
fruitBasket.add('Orange');
fruitBasket.set(3, 'Peach')
for (String i : fruitBasket) {
System.out.println(i);
} } }
indlela ikwi #before replace [Apple, Banana, Grapes, Orange] #after replace [Apple, Banana, Grapes, Peach] class kwaye isetyenziselwa ukwenza ukushukuxa okungahleliwe kwezinto zeArrayList.

shuffle()

Iziphumo:

Collections

Uhlela njani izinto kuluhlu lweArray

import java.util.ArrayList; import java.util.ArrayList; import java.util.Collections; public class ArrayListTutorial { public static void main(String[] args) {
ArrayList fruitBasket = new ArrayList();
fruitBasket.add('Apple');
fruitBasket.add('Banana');
fruitBasket.add('Grapes');
fruitBasket.add('Orange');
System.out.println(fruitBasket);
Collections.shuffle(fruitBasket);
System.out.println(fruitBasket); } }

Iziphumo:


#before shuffle [Apple, Banana, Grapes, Orange] #after shuffle [Grapes, Orange, Banana, Apple]

Uziguqula njani izinto kuLuhlu lweArray

import java.util.ArrayList; import java.util.ArrayList; import java.util.Collections; public class ArrayListTutorial { public static void main(String[] args) {
ArrayList fruitBasket = new ArrayList();
fruitBasket.add('Orange');
fruitBasket.add('Apple');
fruitBasket.add('Grapes');
fruitBasket.add('Banana');
System.out.println(fruitBasket);
Collections.sort(fruitBasket);
System.out.println(fruitBasket); } }

Iziphumo:

#before sort [Orange, Apple, Grapes, Banana] #after sort [Apple, Banana, Grapes, Orange]

Uluguqula njani uluhlu lweArray kuLuhlu

public class ArrayListTutorial { public static void main(String[] args) {
ArrayList fruitBasket = new ArrayList();
fruitBasket.add('Apple');
fruitBasket.add('Banana');
fruitBasket.add('Grapes');
fruitBasket.add('Orange');
System.out.println(fruitBasket);
Collections.reverse(fruitBasket);
System.out.println(fruitBasket); } }

Iziphumo:

#original list [Apple, Banana, Grapes, Orange] #after reverse [Orange, Grapes, Banana, Apple]

Ungazitshintsha njani izinto ezimbini kuLuhlu lweArray

import java.util.ArrayList; public class ArrayListTutorial { public static void main(String[] args) {
ArrayList fruitBasket = new ArrayList();
fruitBasket.add('Apple');
fruitBasket.add('Banana');
fruitBasket.add('Grapes');
fruitBasket.add('Orange');
String[] fruitArray = new String[fruitBasket.size()];
fruitBasket.toArray(fruitArray);
for (String i : fruitArray) {
System.out.println(i);
} } }

Iziphumo:

Apple Banana Grapes Orange

Ungayijoyina njani iArrayLists ezimbini

import java.util.ArrayList; import java.util.Collections; public class ArrayListTutorial { public static void main(String[] args) {
ArrayList fruitBasket = new ArrayList();
fruitBasket.add('Apple');
fruitBasket.add('Banana');
fruitBasket.add('Grapes');
fruitBasket.add('Orange');
System.out.println(fruitBasket1);
Collections.swap(fruitBasket, 1, 3);
System.out.println(fruitBasket); } }

Iziphumo:

#before swap [Apple, Banana, Grapes, Orange] #after swap [Apple, Orange, Grapes, Banana]

Isiphelo

Kule posi ubone imizekelo eyahlukeneyo yezona zinto zixhaphakileyo kwiJava ArrayLists.