Java basics: several methods for converting an array to a list

Source: Internet
Author: User
Document directory
  •  

Reprinted from: http://www.oschina.net/code/snippet_8676_2652

 

In writing Java programs, the conversion between arrays and List objects is often used.
 

[Code]Array-> List (stringarraytest. Java) View Source

 

Print?

01 import java.util.Arrays;
02 import java.util.List;
03 import java.util.ArrayList;
04  
05 public class StringArrayTest
06 {
07    public static void main(String[] args)
08    {
09       String[] words = {"ace", "boom", "crew", "dog", "eon"};
10  
11       List<String> wordList = Arrays.asList(words);
12  
13  
14       for (String e : wordList)
15       {
16          System.out.println(e);
17       }
18    }
19 }
[Code]Silly practice
String[] words = { ... }; List<String> list = new ArrayList<String>(words.length); for (String s : words) {     list.add(s); }

 

  [Code]This method also works.
import java.util.Collections;List myList = new ArrayList();String[] myStringArray = new String[] {"Java", "is", "Cool"};Collections.addAll(myList, myStringArray);

Over!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.