Home > Java > javaTutorial > Java Example - Convert Collection to Array

Java Example - Convert Collection to Array

黄舟
Release: 2017-01-21 11:18:52
Original
1074 people have browsed it

The following example demonstrates how to use the list.add() and list.toArray() methods of the Java Util class to convert a collection into an array:

/*
 author by w3cschool.cc
 Main.java
 */

import java.util.*;

public class Main{
   public static void main(String[] args){
      List<String> list = new ArrayList<String>();
      list.add("菜"); 
      list.add("鸟"); 
      list.add("教");
      list.add("程");
      list.add("www.w3cschool.cc");
      String[] s1 = list.toArray(new String[0]); 
      for(int i = 0; i < s1.length; ++i){
         String contents = s1[i];
         System.out.print(contents);
     } 
   }
}
Copy after login

The output of the above code is: :

菜鸟教程www.w3cschool.cc
Copy after login

The above is the Java example - the content of converting a collection to an array. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template