The toArray() metoda sučelja List vraća niz koji sadrži sve elemente prisutne na popisu pravilnim redoslijedom.
Druga sintaksa vraća niz koji sadrži sve elemente na ovom popisu gdje je tip vremena izvođenja vraćenog niza onaj navedenog niza.
Sintaksa
Object[] toArray() T[] toArray(T[] a)
Parametri
- DA
- Parametar 'a' predstavlja niz u koji će biti pohranjeni elementi liste.
Parametri tipa
Parametar tipa 'T' predstavlja tip komponente polja za držanje zbirke.
Povratna vrijednost
Metoda toArray() vraća niz koji sadrži sve elemente ovog popisa pravilnim redoslijedom.
Odredio
toArray() u sučelju Collection
Bacanja:
ArrayStoreException - Ako tip vremena izvođenja navedenog niza nije supertip tipa vremena izvođenja svakog elementa na ovom popisu.
NullPointerException - Ako je definirani niz null.
Primjer 1
import java.util.LinkedList; import java.util.List; public class JavaListToArrayExample1 { static int j=1; public static void main(String[] args) { List list = new LinkedList(); for (char i='a';i<='z';i++) { list.add(i); } system.out.print('alphabet : '); object[] a="list.toArray();" system.out.println(); for(int i="0" ;i <a.length;i++){ system.out.println( j++ +': '+ a[i]); < pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> Alphabet : 1: a 2: b 3: c 4: d 5: e 6: f 7: g 8: h 9: i 10: j 11: k 12: l 13: m 14: n 15: o 16: p 17: q 18: r 19: s 20: t 21: u 22: v 23: w 24: x 25: y 26: z </pre> <h2>Example 2</h2> <pre> import java.util.LinkedList; import java.util.List; public class JavaListToArrayExample2 { public static void main(String[] args) { List list = new LinkedList(); for (int i = 1; i <= 2="=" 10; i++) { list.add(i); } object[] a="list.toArray();" for (int i="0;" <a.length; system.out.print(a[i] + ' '); if (a[i] % 0) system.out.println(a[i] is an even number.'); < pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> Error:(15, 22) java: bad operand types for binary operator '%' first type: java.lang.Object second type: int </pre> <p>Operator '%' cannot be applied to java.lang.Object. If done so it will give you an error.</p> <h2>Example 3</h2> <p>This example is the solution of the above-described program.</p> <pre> import java.util.LinkedList; import java.util.List; public class JavaListToArrayExample3 { public static void main(String[] args) { List list= new LinkedList(); System.out.println('List of odd numbers in our list.'); for (int i=1;i<=10;i++) { list.add(i); } integer[] a="new" integer[5]; b="list.toArray(a);" for (int i="0;" <b.length; i++) if (b[i] %2!="0)" system.out.println(b[i]+' '); < pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> List of odd numbers in our list. 1 3 5 7 9 </pre></=10;i++)></pre></=></pre></='z';i++)>
Primjer 2
import java.util.LinkedList; import java.util.List; public class JavaListToArrayExample2 { public static void main(String[] args) { List list = new LinkedList(); for (int i = 1; i <= 2="=" 10; i++) { list.add(i); } object[] a="list.toArray();" for (int i="0;" <a.length; system.out.print(a[i] + \' \'); if (a[i] % 0) system.out.println(a[i] is an even number.\'); < pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> Error:(15, 22) java: bad operand types for binary operator '%' first type: java.lang.Object second type: int </pre> <p>Operator '%' cannot be applied to java.lang.Object. If done so it will give you an error.</p> <h2>Example 3</h2> <p>This example is the solution of the above-described program.</p> <pre> import java.util.LinkedList; import java.util.List; public class JavaListToArrayExample3 { public static void main(String[] args) { List list= new LinkedList(); System.out.println('List of odd numbers in our list.'); for (int i=1;i<=10;i++) { list.add(i); } integer[] a="new" integer[5]; b="list.toArray(a);" for (int i="0;" <b.length; i++) if (b[i] %2!="0)" system.out.println(b[i]+\' \'); < pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> List of odd numbers in our list. 1 3 5 7 9 </pre></=10;i++)></pre></=>
Operator '%' ne može se primijeniti na java.lang.Object. Ako to učinite, javit će vam se pogreška.
Primjer 3
Ovaj primjer je rješenje gore opisanog programa.
import java.util.LinkedList; import java.util.List; public class JavaListToArrayExample3 { public static void main(String[] args) { List list= new LinkedList(); System.out.println('List of odd numbers in our list.'); for (int i=1;i<=10;i++) { list.add(i); } integer[] a="new" integer[5]; b="list.toArray(a);" for (int i="0;" <b.length; i++) if (b[i] %2!="0)" system.out.println(b[i]+\' \'); < pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> List of odd numbers in our list. 1 3 5 7 9 </pre></=10;i++)>=10;i++)>=>='z';i++)>