The toArray() metoda vmesnika List vrne matriko, ki vsebuje vse elemente, prisotne na seznamu v pravilnem vrstnem redu.
Druga sintaksa vrne matriko, ki vsebuje vse elemente na tem seznamu, pri čemer je vrsta izvajalnega časa vrnjene matrike tista iz navedene matrike.
Sintaksa
Object[] toArray() T[] toArray(T[] a)
Parametri
- TO
- Parameter 'a' predstavlja matriko, v kateri bodo shranjeni elementi seznama.
Parametri vrste
Parameter tipa 'T' predstavlja tip komponente matrike, ki hrani zbirko.
Povratna vrednost
Metoda toArray() vrne matriko, ki vsebuje vse elemente tega seznama v pravilnem vrstnem redu.
Določeno s strani
toArray() v vmesniku Collection
Meti:
ArrayStoreException - Če tip izvajalnega časa navedene matrike ni nadtip izvajalnega tipa vsakega elementa na tem seznamu.
NullPointerException - Če je definirana matrika ničelna.
Primer 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++)>
Primer 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></=>
Operatorja '%' ni mogoče uporabiti za java.lang.Object. Če je tako, se bo pojavila napaka.
Primer 3
Ta primer je rešitev zgoraj opisanega 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++)>