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 36042a4

Browse files
committed
如何便捷地将两个数组合到一起
1 parent 6cd09c9 commit 36042a4

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ stackoverflow-Java-top-qa
2424
* [如何分割(split)string字符串](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-to-split-a-string-in-java.md)
2525
* [在java中如何对比(compare)string](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-do-i-compare-strings-in-java.md)
2626
* [`Map<Key,Value>`基于Value值排序](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-to-sort-a-mapkey-value-on-the-values-in-java.md)
27+
* [如何便捷地将两个数组合到一起](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-to-concatenate-two-arrays-in-java.md)
2728

2829
> 编程技巧
2930
@@ -75,7 +76,6 @@ stackoverflow-Java-top-qa
7576
- [Comparing Java enum members: == or equals()?](http://stackoverflow.com/questions/1750435/comparing-java-enum-members-or-equals)
7677
- [Failed to load the JNI shared Library (JDK)](http://stackoverflow.com/questions/7352493/failed-to-load-the-jni-shared-library-jdk)
7778
- [Does Java support default parameter values?](http://stackoverflow.com/questions/997482/does-java-support-default-parameter-values)
78-
- [How to concatenate two arrays in Java?](http://stackoverflow.com/questions/80476/how-to-concatenate-two-arrays-in-java)
7979
- [What's the simplest way to print a Java array?](http://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array)
8080
- [Why can't I switch on a String?](http://stackoverflow.com/questions/338206/why-cant-i-switch-on-a-string)
8181
- [How to create a Java String from the contents of a file?](http://stackoverflow.com/questions/326390/how-to-create-a-java-string-from-the-contents-of-a-file)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
##如何便捷地将两个数组合到一起
2+
3+
###一行代码搞定
4+
Apache Commons Lang library [`ArrayUtils.addAll(T[], T...)`](http://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/ArrayUtils.html#addAll%28T%5B%5D,%20T...%29)就是专门干这事的
5+
6+
代码:
7+
```java
8+
String[] both = ArrayUtils.addAll(first, second);
9+
```
10+
11+
###不借助依赖包
12+
13+
####非泛型
14+
把下面的`Foo`替换成你自己的类名
15+
```java
16+
public Foo[] concat(Foo[] a, Foo[] b) {
17+
int aLen = a.length;
18+
int bLen = b.length;
19+
Foo[] c= new Foo[aLen+bLen];
20+
System.arraycopy(a, 0, c, 0, aLen);
21+
System.arraycopy(b, 0, c, aLen, bLen);
22+
return c;
23+
}
24+
```
25+
26+
####泛型
27+
```java
28+
public <T> T[] concatenate (T[] a, T[] b) {
29+
int aLen = a.length;
30+
int bLen = b.length;
31+
32+
@SuppressWarnings("unchecked")
33+
T[] c = (T[]) Array.newInstance(a.getClass().getComponentType(), aLen+bLen);
34+
System.arraycopy(a, 0, c, 0, aLen);
35+
System.arraycopy(b, 0, c, aLen, bLen);
36+
37+
return c;
38+
}
39+
```
40+
注意,泛型的方案不适用于基本数据类型(int,boolean......)

0 commit comments

Comments
(0)

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