Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5a36139

Browse files
committed
create-arraylist-arraylistt-from-array-t
1 parent 5663c29 commit 5a36139

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

‎README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ stackoverflow-Java-top-qa
1111
* [Java是按值传递还是按引用传递](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/is-java-pass-by-reference-or-pass-by-value.md)
1212
* [Java += 操作符实质](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/java-operator.md)
1313
* [将InputStream转换为String](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/read-convert-an-inputstream-to-a-string.md)
14-
14+
*[将数组转换为List](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/create-arraylist-arraylistt-from-array-t.md)
1515

1616
> 代码规范
1717
@@ -32,7 +32,6 @@ stackoverflow-Java-top-qa
3232
- [Differences between HashMap and Hashtable?](http://stackoverflow.com/questions/40471/differences-between-hashmap-and-hashtable)
3333
- [Creating a memory leak with Java [closed]](http://stackoverflow.com/questions/6470651/creating-a-memory-leak-with-java)
3434
- [Iterate through a HashMap [duplicate]](http://stackoverflow.com/questions/1066589/iterate-through-a-hashmap)
35-
- [Create ArrayList (ArrayList<T>) from array (T[])](http://stackoverflow.com/questions/157944/create-arraylist-arraylistt-from-array-t)
3635
- [Why is char[] preferred over String for passwords?](http://stackoverflow.com/questions/8881291/why-is-char-preferred-over-string-for-passwords)
3736
- [Generating random integers in a range with Java](http://stackoverflow.com/questions/363681/generating-random-integers-in-a-range-with-java)
3837
- [Why is printing "B" dramatically slower than printing "#"?](http://stackoverflow.com/questions/21947452/why-is-printing-b-dramatically-slower-than-printing)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
##将数组转换为List
2+
3+
###问题
4+
假设有数组
5+
```java
6+
Element[] array = {new Element(1),new Element(2),new Element(3)};
7+
```
8+
如何将其转换为ArrayList<Element> arraylist呢?
9+
10+
###回答
11+
Arrays.asList(array)或者Arrays.asList(new Element(1),new Element(2),new Element(3))
12+
不过,这样做有些坑要注意:
13+
1. 这样做生成的list,是定长的。也就是说,如果你对它做add或者remove,都会抛UnsupportedOperationException。
14+
2. 如果修改数组的值,list中的对应值也会改变!
15+
16+
如果希望避免这两个坑,请改用这个方式
17+
Collections.addAll(arraylist, array);
18+
19+
stackoverflow原址:
20+
http://stackoverflow.com/questions/157944/how-to-create-arraylist-arraylistt-from-array-t

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /