Skip to content

Commit e0f2dbb

Browse files
committed
【new add】
1.E:\MyBlogAndCode\BlogAndCode\Java\ArrayList的toArray方法.md 2.Life/Gnosis/技术文档编写模版.md
1 parent ccf5511 commit e0f2dbb

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

Java/ArrayList的toArray方法.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# ArrayList的toArray()方法详解
2+
## 背景
3+
工作一年多了,似是而非的接触Java已经一年,每天忙于堆业务,改Bug,没时间去学习Java的基础知识,仅仅局限于会使用Java语言,这样子肯定是不行的。今天无意间看到一个Android开源Library,里面有这样一段代码:
4+
```
5+
public void notifyUpdateSkin(Object arg) {
6+
SkinObserver[] arrLocal;
7+
8+
synchronized (this) {
9+
arrLocal = observers.toArray(new SkinObserver[observers.size()]);
10+
}
11+
12+
for (int i = arrLocal.length-1; i>=0; i--)
13+
arrLocal[i].updateSkin(this, arg);
14+
}
15+
```
16+
看了半天居然没看懂`arrLocal = observers.toArray(new SkinObserver[observers.size()]);`
17+
这行代码,也就是没看懂`toArray()`方法。什么都别说了,赶紧努力学习。
18+
## 知识详解
19+
`ArrayList`提供了一个将`List`转化为数组的方法`toArray``toArray`有两个重载的方法:
20+
* list.toArray();
21+
* list.toArray(T[] a);
22+
第一个方法是将`List`直接转化为`Object[]`数组;
23+
第二个方法是将`List`转化为你需要类型的数组,就是直接转化为T[]类型,但是肯定和存储在列表中的类型是相同的才行。
24+
25+
## 问题
26+
```
27+
ArrayList<String> ll = new ArrayList<>();
28+
for (int index = 0; index < 10; index++) {
29+
ll.add(""+index);
30+
}
31+
String[] array= (String[]) ll.toArray();
32+
```
33+
如果我们想上面那样使用会报如下错误:
34+
```
35+
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;
36+
```
37+
不能将`Object[]`转化为`String[]`类型,如果要转化就只能将每个元素进行逐一转化:
38+
```
39+
Object[] objects = ll.toArray();
40+
for (int index = 0; index < objects.length; index++) {
41+
String eString = (String) objects[index];
42+
}
43+
```
44+
所以第二个方法就可以直接解决上述的问题:
45+
```
46+
ll.toArray(new String[ll.size()]);
47+
```
48+
## 源码解析
49+
* toArray()
50+
```
51+
public Object[] toArray() {
52+
return Arrays.copyOf(elementData, size);
53+
}
54+
```
55+
* toArray(T[] a)
56+
```
57+
public <T> T[] toArray(T[] a) {
58+
if (a.length < size)
59+
// Make a new array of a's runtime type, but my contents:
60+
return (T[]) Arrays.copyOf(elementData, size, a.getClass());
61+
System.arraycopy(elementData, 0, a, 0, size);
62+
if (a.length > size)
63+
a[size] = null;
64+
return a;
65+
}
66+
```
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# 标题:
2+
## 背景:
3+
##

0 commit comments

Comments
 (0)