logo

Java sąrašo toArray() metodas

The toArray () Sąrašo sąsajos metodas grąžina masyvą, kuriame yra visi sąraše esantys elementai tinkama tvarka.

Antroji sintaksė grąžina masyvą, kuriame yra visi šio sąrašo elementai, kur grąžinamo masyvo vykdymo laikas yra nurodyto masyvo.

Sintaksė

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

Parametrai

  1. TAI
  2. Parametras „a“ reiškia masyvą, kuriame bus saugomi sąrašo elementai.

Tipo parametrai

Tipo parametras „T“ nurodo masyvo komponento tipą, kuriame saugoma kolekcija.

Grąžinimo vertė

Metodas toArray() grąžina masyvą, kuriame yra visi šio sąrašo elementai tinkama tvarka.

Nurodė

toArray () sąsajos kolekcijoje

Metimai:

ArrayStoreException - Jei nurodyto masyvo vykdymo laikas nėra kiekvieno šio sąrašo elemento vykdymo tipo supertipas.

NullPointerException - Jei apibrėžtas masyvas yra nulinis.

1 pavyzdys

 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 pavyzdys

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

Operatoriaus „%“ negalima pritaikyti java.lang.Object. Jei tai padarysite, gausite klaidą.

3 pavyzdys

Šis pavyzdys yra aukščiau aprašytos programos sprendimas.

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