Java ArrayList Maitiro Ane Mienzaniso

Mune ino posvo isu tinofukidza zvakajairika nzira dzeJava ArrayList ine mienzaniso yekodhi yeimwe nzira.

I ArrayList kirasi iri inogadziriswazve. Isu tinowanzoshandisa ArrayList apo isu tisingazive saizi yechinyorwa zvisati zvaitika.

I ArrayList kirasi iri mu java.util package.

Nzira yekugadzira ArrayList

Kugadzira ArrayList uye nekuwedzera zvinhu kwazviri, tinosimbisa ArrayList chinhu chine mhando yakatarwa, senge String kana Integer.

Cherekedza iyo add() nzira inowedzera chinhu kuchinzvimbo chinotevera mune iyo ArrayList. Ona muenzaniso unotevera kuti uone maitiro ekuwedzera chinhu kune yakatarwa index.

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); } }

Kubuda:



[Apple, Banana, Grapes, Orange]

Maitiro Ekuwedzera Icho Chinhu kune Chaicho Chinzvimbo

Kuti tiwedzere chinhu pachinzvimbo chakatsanangurwa, isu tinofanirwa kupa indekisi inodikanwa kune iyo _ _ + _ | nzira.

add()

Kubuda:

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); } }

Maitiro Ekuwana Icho Chinhu mune ArrayList

I [Apple, Banana, Melon, Grapes, Orange] nzira inowana chinhu. Isu tinofanirwa kupa iyo index index kune get() nzira.

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)); } }

Maitiro Ekubvisa Icho Chinhu Kubva kuArrayList

Kuti tibvise chinhu, tinoshandisa Banana nzira. Isu tinofanirwa kupa iyo index nhamba.

remove()

Kubuda:

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); } }

Maitiro Ekubvisa Zvese Zvinhu Kubva kuArrayList

I [Banana, Grapes, Orange] nzira inobvisa zvinhu zvese kubva clear().

ArrayListimport 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); } }

Maitiro Ekuwana Saizi yeArrayList

[]

Kubuda:

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()); } }

Maitiro Ekusunungura kuburikidza neArrayList

4

Kubuda:

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));
} } }

Kusunungura kuburikidza neArrayList Ine ye-yega

Apple Banana Grapes Orange

Kubuda:

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);
} } }

ArrayList Muenzaniso Nenhamba

Apple Banana Grapes Orange

Kubuda:

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);
} } }

Maitiro Ekutsiva icho Chinhu mune ArrayList

I 10 20 30 40 nzira inotsiva chinhu nechimwe chinhu chakapihwa. Tinofanirwa kudoma indekisi yechinhu chatiri kuda kutsiva.

replace()

Kubuda:

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);
} } }

Maitiro Ekusanganisa Zvinhu muArrayList

I #before replace [Apple, Banana, Grapes, Orange] #after replace [Apple, Banana, Grapes, Peach] nzira iri mu shuffle() class uye inoshandiswa kuita kusarongeka shuffle yezvinhu zveArrayList.

Collections

Kubuda:

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); } }

Maitiro Ekurongedza Zvinhu mune ArrayList

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

Kubuda:

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); } }

Maitiro Ekudzoreredza Izvo Zvinhu pane ArrayList

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

Kubuda:

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); } }

Maitiro ekushandura ArrayList kuita Array

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

Kubuda:

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);
} } }

Maitiro ekuchinjisa Zvinhu zviviri mune ArrayList

Apple Banana Grapes Orange

Kubuda:

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); } }

Maitiro Ekujoinha maviri ArrayLists

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

Kubuda:

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

Mhedziso

Mune ino posvo iwe waona mienzaniso yakasiyana yeakajairika mashandiro pa Java ArrayLists.