Metode Dhaptar Array Java Kanthi Tuladhane

Ing kiriman iki, kita nutupi cara ArrayList Java sing paling umum kanthi conto kode kanggo saben metode.

Sing ArrayList kelas minangka larik sing bisa diowahi ukurane. Biasane nggunakake ArrayList nalika kita durung ngerti ukuran dhaptar sadurunge.

Sing ArrayList kelas ana ing java.util bungkus




Cara Nggawe ArrayList

Kanggo nggawe ArrayList lan tambahake item, kita instal ArrayList obyek kanthi jinis sing ditemtokake, kayata String utawa Integer.

Elingi ing add() cara nambah item menyang posisi sabanjure ing ArrayList. Deleng conto sabanjure kanggo ndeleng cara nambah item menyang indeks tartamtu.


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

Output:



[Apple, Banana, Grapes, Orange]

Cara Nambahake Barang menyang Posisi Khusus

Kanggo nambah item menyang posisi sing ditemtokake, kita kudu nyedhiyani indeks sing dipengini kanggo add() cara

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

Output:

[Apple, Banana, Melon, Grapes, Orange]

Cara Ngakses Item ing ArrayList

Sing get() cara njupuk item. Kita kudu nyediakake nomer indeks menyang get() cara


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

Cara Mbusak Barang Saka Dhaptar Array

Kanggo mbusak item, kita nggunakake remove() cara Kita kudu nyedhiyani nomer indeks.

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

Output:

[Banana, Grapes, Orange]

Cara Mbusak Kabeh Barang Saka Dhaptar Array

Sing clear() cara mbusak kabeh item saka ArrayList.

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

Cara Golek Ukuran Dhaptar Array

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

Output:


4

Cara Loop Liwat ArrayList

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

Output:

Apple Banana Grapes Orange

Looping Liwat ArrayList Kanthi-kanggo

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

Output:

Apple Banana Grapes Orange

Tuladha Dhaptar Array Kanthi Nomer

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

Output:

10 20 30 40

Cara Ngganti Item ing Dhaptar Array

Sing replace() cara ngganti item karo item liyane sing disedhiyakake. Kita kudu nemtokake indeks item sing pengin diganti.


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

Output:

#before replace [Apple, Banana, Grapes, Orange] #after replace [Apple, Banana, Grapes, Peach]

Cara Ngacak Barang ing ArrayList

Sing shuffle() cara ing Collections kelas lan digunakake kanggo nindakake acak acak item ArrayList.

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

Output:

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

Cara Ngurutake Barang ing Dhaptar Array

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

Output:


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

Cara Mbalikke Barang ing 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);
Collections.reverse(fruitBasket);
System.out.println(fruitBasket); } }

Output:

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

Cara Ngonversi Dhaptar Array dadi Array

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

Output:

Apple Banana Grapes Orange

Cara Ngonversi Loro Barang ing Dhaptar Array

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

Output:

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

Cara Nggabungake Loro Dhaptar

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

Output:

[Apple, Banana, Grapes, Orange, Melon, Strawberries]

Kesimpulan

Ing kiriman iki sampeyan ndeleng macem-macem conto operasi sing paling umum ing Dhaptar Array Java.