The ArrayList method toArray() used to create an array containing all of the elements in this list in proper sequence. It returns an array containing all of the elements in this list in proper sequence.
1 public Object[] toArray()
return : It returns an array containing all of the elements in this list in proper sequence.
ArrayList method toArray()
1 package iogyan.example;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5
6 public class Main {
7
8 public static void main(String[] args) {
9
10
11 ArrayList<Integer> alist = new ArrayList<>();
12
13 alist.addAll(Arrays.asList(20, 10, 30, 40, 30));
14 System.out.println("A list elements : " + alist);
15
16 Object[] array = alist.toArray();
17 for(Object obj : array) {
18 Integer element = (Integer) obj;
19 System.out.print(element + " ");
20 }
21 }
22 }
In the above example, a ArrayList of type integer created and elements added using addAll() by passing a list. A toArray() method called that returns array of objects. A resulting array iterated using enhance for loop and an element cast to Integer and print separated white space.
1 A list elements : [20, 10, 30, 40, 30]
2 20 10 30 40 30
ArrayList method toArray() with String
ArrayList method toArray() with String
1 package iogyan.example;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5
6 public class StringMain {
7
8 public static void main(String[] args) {
9
10
11 ArrayList<String> alist = new ArrayList<>();
12 alist.addAll(Arrays.asList("Welcome", "IOGyan", "thanks"));
13 System.out.println("A list elements : " + alist);
14
15 Object[] array = alist.toArray();
16 for(Object obj : array) {
17 String element = (String) obj;
18 System.out.print(element.toUpperCase() + " ");
19 }
20 }
21 }
In the above example, a ArrayList of type String created and elements added using addAll() by passing a list. A toArray() method called that returns array of objects. A resulting array iterated using enhance for loop and an element cast to String and print element by converting to uppercase, separated white space.
1 A list elements : [Welcome, IOGyan, thanks]
2 WELCOME IOGYAN THANKS
ArrayList method toArray() with custom object
ArrayList method toArray() with custom object
1 package iogyan.example;
2
3 import iogyan.model.Employee;
4
5 import java.util.ArrayList;
6 import java.util.Arrays;
7
8 public class EmployeeMain {
9
10 public static void main(String[] args) {
11
12 Employee emp1 = new Employee(1, "Emp1","Tech", 1000);
13 Employee emp2 = new Employee(2, "Emp2","Admin", 5000);
14 Employee emp3 = new Employee(3, "Emp3","Admin", 12000);
15
16
17 ArrayList<Employee> alist = new ArrayList<>();
18 alist.addAll(Arrays.asList(emp1, emp2, emp3));
19 System.out.println("A list elements : " + alist);
20
21 Object[] array = alist.toArray();
22 for(Object obj : array) {
23 Employee emp = (Employee) obj;
24 System.out.println(emp.getName() + " -> " + emp.getSalary());
25 }
26 }
27 }
In the above example, a ArrayList of type Employee created and elements added using addAll() by passing a list. A toArray() method called that returns array of objects. A resulting array iterated using enhance for loop and an element cast to Employee and print employee name and salary.
1 A list elements : [Emp1 Tech 1000.0, Emp2 Admin 5000.0, Emp3 Admin 12000.0]
2 Emp1 -> 1000.0
3 Emp2 -> 5000.0
4 Emp3 -> 12000.0
ArrayList method toArray() with array as parameter
The ArrayList method toArray() with array as parameter used converts an array containing all of the elements in this list in proper sequence. If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list. It returns an array containing the elements of the list.
1 public <T> T[] toArray(T[] a)
a : It is the array into which the elements of the list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
return : It returns an array containing the elements of the list.
Exceptions:
1. | ArrayStoreException : if the runtime type of the specified array is not a supertype of the runtime type of every element in this list |
2. | NullPointerException : if the specified array is null |
ArrayList method toArray() with array as same size of list
1 package iogyan.example;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5
6 public class Main {
7
8 public static void main(String[] args) {
9
10
11 ArrayList<Integer> alist = new ArrayList<>();
12
13 alist.addAll(Arrays.asList(20, 10, 30, 40, 30));
14 System.out.println("A list elements : " + alist);
15
16
17 Integer[] array = new Integer[alist.size()];
18 Integer[] resArr = alist.toArray(array);
19
20 System.out.println("A resulting array : " + Arrays.toString(resArr));
21 System.out.println("Does both array are same : " + (array == resArr));
22 }
23 }
In the above example, an array created with same size as list and passed as parameter to toArray() method, it copy all of the list elements into the array and returns the same array. A string presentation of the array print using toString() method. An arrays compared using double equals operator that compares the reference and returns boolean value true.
1 A list elements : [20, 10, 30, 40, 30]
2 A resulting array : [20, 10, 30, 40, 30]
3 Does both array are same : true
ArrayList method toArray() with array as less size of list
ArrayList method toArray() with array as less size of list
1 package iogyan.example;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5
6 public class Main {
7
8 public static void main(String[] args) {
9
10
11 ArrayList<Integer> alist = new ArrayList<>();
12
13 alist.addAll(Arrays.asList(20, 10, 30, 40, 30));
14 System.out.println("A list elements : " + alist);
15
16
17 Integer[] array = new Integer[2];
18 Integer[] resArr = alist.toArray(array);
19
20 System.out.println("A resulting array : " + Arrays.toString(resArr));
21 System.out.println("Does both array are same : " + (array == resArr));
22 }
23 }
In the above example, an array created with size 2 and passed as parameter to toArray() method, it creates new array as list elements not fit into the specified array. It copies all elements into the new array and return that print using toString() method. An arrays compared using double equals operator that compares the reference and returns boolean value false.
1 A list elements : [20, 10, 30, 40, 30]
2 A resulting array : [20, 10, 30, 40, 30]
3 Does both array are same : false
Employee class
1 package iogyan.model;
2
3 public class Employee implements Comparable<Employee> {
4 private final int id;
5 private final String name;
6 private final String dept;
7 private final double salary;
8
9 public Employee(int id, String name, String dept, double salary) {
10 this.id = id;
11 this.name = name;
12 this.dept = dept;
13 this.salary = salary;
14 }
15
16 public int getId() {
17 return this.id;
18 }
19
20 public String getName() {
21 return this.name;
22 }
23
24 public String getDept() {
25 return this.dept;
26 }
27
28 public double getSalary() {
29 return this.salary;
30 }
31
32 @Override
33 public int hashCode() {
34 return super.hashCode();
35 }
36
37 @Override
38 public boolean equals(Object obj) {
39 return this.id == ((Employee)obj).id;
40 }
41
42 @Override
43 public String toString() {
44 return this.getName() + " " + this.getDept() + " " + this.getSalary();
45 }
46
47 @Override
48 public int compareTo(Employee o) {
49 return this.getId() - o.getId();
50 }
51 }
Related options for your search