Stackoverflow: convert an array to List and stackoverflowlist.

Source: Internet
Author: User

Stackoverflow: convert an array to List and stackoverflowlist.
ProblemSuppose there is an array

Element[] array = {new Element(1),new Element(2),new Element(3)};
How to convert it to ArrayList <Element> arraylist?

Excellent answer
Arrays. asList (array) orArrays. asList (new Element (1), new Element (2), new Element (3 ))
However, some pitfalls should be noted: 1. The list generated in this way is fixed length. That is to say, if you add or remove it, UnsupportedOperationException will be thrown. 2. If you modify the array value, the corresponding value in the list will also change!
If you want to avoid these two pitfalls, use this method instead.
Collections.addAll(arraylist, array);
Original Site of stackoverflow:Http://stackoverflow.com/questions/157944/how-to-create-arraylist-arraylistt-from-array-t


Column introduction: I like stackoverflow very much and can always find a solution to the problem. The website has a popularity list. So I selected some hot questions, read all the answers to each question, and sort them out based on my own understanding. We hope that the above discussion will be presented in a more streamlined and appropriate manner. If you need to reprint, please specify the original address Http://blog.csdn.net/lizeyang

Converting list to String Array

For (int I = 0; I <s. length; I ++) {// Add the items in the s array to the list.
List. add (s [I]);
}

List. toArray (); // you can directly convert a list to an array. I cannot remember the method name clearly. You can check the api

Microsoft visual studio, I only defined a very large array a [116674]. Why is stackoverflowexception?

Stack Overflow ~

Try ArrayList ~
 

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.