logo

Java List toArray() metode

Det toArray() metode til listegrænseflade returnerer et array, der indeholder alle elementerne på listen i den rigtige rækkefølge.

Den anden syntaks returnerer en matrix, der indeholder alle elementerne på denne liste, hvor runtime-typen for den returnerede matrix er den for den angivne matrix.

Syntaks

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

Parametre

  1. AT
  2. Parameteren 'a' repræsenterer det array, hvori listens elementer vil blive gemt.

Type parametre

Typeparameteren 'T' repræsenterer komponenttypen for det array, der skal holde samlingen.

Returværdi

Metoden toArray() returnerer et array, der indeholder alle elementerne i denne liste i den rigtige rækkefølge.

Specificeret af

toArray() i interface Collection

Kaster:

ArrayStoreException - Hvis runtime-typen for det angivne array ikke er en supertype af runtime-typen for hvert element på denne liste.

NullPointerException - Hvis det definerede array er null.

Eksempel 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=&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++)>

Eksempel 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 &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></=>

Operator '%' kan ikke anvendes på java.lang.Object. Hvis du gør det, vil det give dig en fejl.

Eksempel 3

Dette eksempel er løsningen af ​​det ovenfor beskrevne program.

 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++)>