ArrayList.toArray()

ArrayList

ArrayList.toArray() get an array from the ArrayList.
It returns the array.

Examples with output

add()
ArrayList<String> languages = new ArrayList<String>();
languages.add("Java");
languages.add("PHP");
languages.add("HTML");
System.out.println(languages); //[Java, PHP, HTML]
Object[] my_array = languages.toArray();

System.out.println(" Printing array elements "); 

for (Object value : my_array) {
	System.out.println(value); 
}
Output is here
[Java, PHP, HTML]
 Printing array elements 
Java
PHP
HTML
Integer array
ArrayList<Integer> marks = new ArrayList<Integer>();
marks.add(54);
marks.add(55);
marks.add(56);

System.out.println(marks); //[Java, PHP, HTML]

Object[]  my_array=marks.toArray();
System.out.println(" Printing array elements "); 

for (Object value : my_array) {
	System.out.println(value); 
}
Output
[54, 55, 56]
 Printing array elements 
54
55
56
Using size() to get the number of elements and use the same to declare the array.
ArrayList<Integer> marks = new ArrayList<Integer>();
marks.add(54);
marks.add(55);
marks.add(56);

System.out.println(marks); //[Java, PHP, HTML]

Integer my_array[] = new Integer[marks.size()]; 
my_array = marks.toArray(my_array); 

System.out.println(" Printing array elements "); 

for (Object value : my_array) {
	System.out.println(value); 
}
Output
[54, 55, 56]
 Printing array elements 
54
55
56
ArrayList Tutorials



plus2net.com



Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2020 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer