Guqula uluhlu lube luhlu kwiJava

Ukuguqula phakathi koLuhlu kunye neArray yinto eqhelekileyo eJava.

Eyona ndlela ilula kunye neyona ilula yokuguqula uluhlu lube luhlu kwiJava kukusebenzisa .toArray() indlela.

Kwangokunjalo, sinako ukuguqula uluhlu lube luhlu lusebenzise Arrays.asList() indlela.


Imizekelo engezantsi ibonisa indlela yokuguqula uLuhlu loMtya kunye noLuhlu lweeNgcaciso kwi-Array efanayo.



Guqula uluhlu lube luluhlu lomtya

import java.util.ArrayList; import java.util.List; public class ConvertArrayListToArray {
public static void main(String[] args) {
List itemList = new ArrayList();
itemList.add('item1');
itemList.add('item2');
itemList.add('item3');

String[] itemsArray = new String[itemList.size()];
itemsArray = itemList.toArray(itemsArray);

for(String s : itemsArray)

System.out.println(s);
} }

Singasebenzisa indlela efanayo ukuguqula uluhlu lwenani elipheleleyo libe luluhlu lweenombolo ezipheleleyo, umzekelo:


Idibeneyo:





Guqula uluhlu lube luhlu lweenombolo ezipheleleyo

import java.util.ArrayList; import java.util.List; public class ConvertArrayListToArray {
public static void main(String[] args) {
List intList = new ArrayList();
intList.add(10);
intList.add(20);
intList.add(30);

Integer[] intArray = new Integer[intList.size()];
intArray = intList.toArray(intArray);

for(Integer i : intArray)

System.out.println(i);
} }


Guqula uluhlu lomtya lube kuLuhlu

Ungaguqula iArray ibuyele kuLuhlu. Ukwenza oku, sisebenzisa Arrays.asList(). Umzekelo:

import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class ConvertArrayToList {
public static void main(String[] args) {
String[] stringArray = {'item 1', 'item 2', 'item 3', 'item 4'};
List stringList = new ArrayList(Arrays.asList(stringArray));

for (String listItem : stringList) {

System.out.println(listItem);
}
} }