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

Java Example - Convert Collection to Array

黄舟
黄舟Original
2017-01-21 11:18:521040browse

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);
     } 
   }
}

The output of the above code is: :

菜鸟教程www.w3cschool.cc

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


Statement:
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