Movatterモバイル変換


[0]ホーム

URL:


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

Commitf26f7c9

Browse files
committed
Add generic n array concatenation snippet
1 parentf714c21 commitf26f7c9

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

‎README.md‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Update the sample application with the snippet and add a test for it. After prov
1111

1212
###Array
1313
*[Generic two array concatenation](#generic-two-array-concatenation)
14+
*[Generic N array concatenation](#generic-N-array-concatenation)
1415

1516
##Array
1617

@@ -25,3 +26,23 @@ Update the sample application with the snippet and add a test for it. After prov
2526
```
2627

2728
[⬆ back to top](#table-of-contents)
29+
30+
###Generic N array concatenation
31+
32+
```java
33+
publicstatic<T>T[] nArrayConcat(T[] first,T[]... rest) {
34+
int totalLength= first.length;
35+
for (T[] array: rest) {
36+
totalLength+= array.length;
37+
}
38+
T[] result=Arrays.copyOf(first, totalLength);
39+
int offset= first.length;
40+
for (T[] array: rest) {
41+
System.arraycopy(array,0, result, offset, array.length);
42+
offset+= array.length;
43+
}
44+
return result;
45+
}
46+
```
47+
48+
[⬆ back to top](#table-of-contents)

‎src/main/java/Library.java‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,26 @@ public static <T> T[] arrayConcat(T[] first, T[] second) {
1818
System.arraycopy(second,0,result,first.length,second.length);
1919
returnresult;
2020
}
21+
22+
/**
23+
* Generic N array concatenation
24+
* Credits: Joachim Sauer https://stackoverflow.com/questions/80476/how-can-i-concatenate-two-arrays-in-java
25+
* @param first is the first array (not null)
26+
* @param rest the rest of the arrays (optional)
27+
* @param <T> the element type
28+
* @return concatenated array
29+
*/
30+
publicstatic <T>T[]nArrayConcat(T[]first,T[]...rest) {
31+
inttotalLength =first.length;
32+
for (T[]array :rest) {
33+
totalLength +=array.length;
34+
}
35+
T[]result =Arrays.copyOf(first,totalLength);
36+
intoffset =first.length;
37+
for (T[]array :rest) {
38+
System.arraycopy(array,0,result,offset,array.length);
39+
offset +=array.length;
40+
}
41+
returnresult;
42+
}
2143
}

‎src/test/java/LibraryTest.java‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,21 @@ public void testArrayConcat() {
2222
// expected behaviour, everything is fine
2323
}
2424
}
25+
26+
/**
27+
* Tests for {@link Library#nArrayConcat(Object[], Object[][])}
28+
*/
29+
@Test
30+
publicvoidtestNArrayConcat() {
31+
Integer[]single =Library.nArrayConcat(newInteger[1]);
32+
assertEquals(single.length,1);
33+
String[]multiple =Library.nArrayConcat(newString[5],newString[12],newString[3],newString[8]);
34+
assertEquals(multiple.length,28);
35+
try {
36+
Double[]doubles =Library.nArrayConcat(null,null,null,null);
37+
fail();
38+
}catch (NullPointerExceptione) {
39+
// expected behaviour, everything is fine
40+
}
41+
}
2542
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp