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

Java Example - Array to Collection

黄舟
Release: 2017-01-21 10:55:42
Original
1515 people have browsed it

The following example demonstrates using the Arrays.asList(name) method of the Java Util class to convert an array into a collection:

/*
 author by w3cschool.cc
 ArrayToCollection.java
 */import java.util.*;import java.io.*;public class ArrayToCollection{
   public static void main(String args[]) 
   throws IOException{
      int n = 5;         // 5 个元素
      String[] name = new String[n];
      for(int i = 0; i < n; i++){
         name[i] = String.valueOf(i);
      }
      List<String> list = Arrays.asList(name); 
      System.out.println();
      for(String li: list){
         String str = li;
         System.out.print(str + " ");
      }
   }}
Copy after login

The output result of running the above code is:

0 1 2 3 4
Copy after login

above It is the content of Java Example - Array to Collection. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!


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