logo

Java saraksta toArray() metode

The toArray() Saraksta saskarnes metode atgriež masīvu, kurā ir visi sarakstā esošie elementi pareizā secībā.

Otrā sintakse atgriež masīvu, kurā ir visi šī saraksta elementi, kur atgrieztā masīva izpildlaika veids ir norādītā masīva izpildlaika veids.

Sintakse

 Object[] toArray() T[] toArray(T[] a) 

Parametri

  1. TAS
  2. Parametrs “a” apzīmē masīvu, kurā tiks saglabāti saraksta elementi.

Tipa parametri

Tipa parametrs “T” apzīmē masīva komponenta tipu, kurā glabājas kolekcija.

Atdeves vērtība

Metode toArray() atgriež masīvu, kurā ir visi šī saraksta elementi pareizā secībā.

Norādījis

toArray() saskarnes kolekcijā

Metieni:

ArrayStoreException - Ja norādītā masīva izpildlaika tips nav katra šī saraksta elementa izpildlaika tipa supertips.

NullPointerException - Ja definētais masīvs ir nulle.

1. piemērs

 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=&apos;a&apos;;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 &apos;%&apos; first type: java.lang.Object second type: int </pre> <p>Operator &apos;%&apos; 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(&apos;List of odd numbers in our list.&apos;); 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++)>

2. piemērs

 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 &apos;%&apos; first type: java.lang.Object second type: int </pre> <p>Operator &apos;%&apos; 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(&apos;List of odd numbers in our list.&apos;); 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></=>

Operatoru “%” nevar lietot java.lang.Object. Ja tas tiks izdarīts, tiks parādīta kļūda.

3. piemērs

Šis piemērs ir iepriekš aprakstītās programmas risinājums.

 import java.util.LinkedList; import java.util.List; public class JavaListToArrayExample3 { public static void main(String[] args) { List list= new LinkedList(); System.out.println(&apos;List of odd numbers in our list.&apos;); 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++)>