Java ArrayList Methods Me Nga Tauira

I tenei panui ka hipokina e maatau nga tikanga Java ArrayList e mohiotia ana me nga tauira waehere mo ia tikanga.

Ko te ArrayList akomanga he ngohi whakarereke. Te tikanga e whakamahia ana e matou ArrayList ka kore tatou e mohio ki te rahi o te raarangi i mua.

Ko te ArrayList kei roto te akomanga i te java.util mōkihi.

Me Pehea te Hanga i tetahi Rarangi Array

Ki te hanga i tetahi Raarangi Array me te taapiri i nga taonga ki a ia, ka whakauruhia e matou he ArrayList ahanoa me te momo kua tohua, penei i te String ko Integer.

Kia mahara ki te add() ka taapirihia e te tikanga tetahi taonga ki te tunga ka whai ake i te Raarangi Raarangi. Tirohia te tauira e whai ake nei kia kite koe me pehea te taapiri i tetahi taonga ki tetahi taupū motuhake.

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

Putanga:



[Apple, Banana, Grapes, Orange]

Me pehea te Taapiri i tetahi Mea ki tetahi Tuunga Motuhake

Hei taapiri i tetahi mea ki tetahi tuunga kua whakaritea, me whakarato e matou te taupū e hiahiatia ana ki te add() tikanga.

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

Putanga:

[Apple, Banana, Melon, Grapes, Orange]

Me pehea te urunga ki tetahi Mea i roto i te Raarangi Raarangi

Ko te get() tikanga ka tango i tetahi taonga. Me whakarato e matou te nama taupū ki te get() tikanga.

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

Me pehea te tango i tetahi taonga mai i te Raarangi Raarangi

Hei tango i tetahi taonga, ka whakamahia te remove() tikanga. Me whakarato e matou te nama taupū.

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

Putanga:

[Banana, Grapes, Orange]

Me Pehea te Tango i nga Mea Katoa Mai i te Raarangi Array

Ko te clear() ma te tikanga e tango nga mea katoa mai i te 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); } }
[]

Me Pehea Te Raina o te Raarangi Raarangi

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

Putanga:

4

Me Pehea Te Koromuri Ma Te Raarangi Raarangi

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

Putanga:

Apple Banana Grapes Orange

Koropiko Ma Te Raarangi Raarangi Ma te-ia

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

Putanga:

Apple Banana Grapes Orange

Tauira Raarangi Array Me nga Tau

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

Putanga:

10 20 30 40

Me Pehea te Whakapiu i tetahi Mea ki te Raarangi Raarangi

Ko te replace() whakakapi te tikanga i tetahi taonga ki tetahi atu taonga kua tohaina. Me taatai ​​e maatau te tohu mo te taonga e hiahia ana matou ki te whakakapi.

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

Putanga:

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

Me Pehea te Riwhi i nga Mea i te Raarangi Array

Ko te shuffle() kei roto te tikanga i te Collections akomanga ana ka whakamahia hei huri i nga taonga 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); } }

Putanga:

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

Me Pehea te Whakaraupapa i nga Mea i te Raarangi 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); } }

Putanga:

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

Me Pehea te Whakaaro i nga Mea i te Raarangi Raarangi

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

Putanga:

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

Me pehea te huri i tetahi Raarangi Array ki te Huinga

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

Putanga:

Apple Banana Grapes Orange

Me Pehea te Huri i nga Mea e Rua ki te Raarangi 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); } }

Putanga:

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

Me pehea te hono atu ki nga Rarangi Raarangi e Rua

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

Putanga:

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

Whakamutunga

I tenei whakairinga i kite koe i nga tauira rereke o nga mahi tino noa kei runga i nga Raarangi Array Java.