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

Commitf0fcb7a

Browse files
committed
Add generic two array concatenation snippet
1 parentc0526fc commitf0fcb7a

File tree

3 files changed

+53
-11
lines changed

3 files changed

+53
-11
lines changed

‎README.md‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
11
#Java Snippets
22

33
Inspired by[30 seconds of code](https://github.com/Chalarangelo/30-seconds-of-code), this is a collection of reusable Java code snippets.
4+
5+
##How to contribute
6+
Update the sample application with the snippet and add a test for it. After proving that it works update this README.md.
7+
8+
##Table of Contents
9+
10+
###Array
11+
*[Generic two array concatenation](#generic-two-array-concatenation)
12+
13+
##Array
14+
15+
###Generic two array concatenation
16+
17+
```java
18+
publicstatic<T>T[] arrayConcat(T[] first,T[] second) {
19+
T[] result=Arrays.copyOf(first, first.length+ second.length);
20+
System.arraycopy(second,0, result, first.length, second.length);
21+
return result;
22+
}
23+
```
24+
25+
[⬆ back to top](#table-of-contents)

‎src/main/java/Library.java‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
1+
importjava.util.Arrays;
2+
13
/*
2-
* This Java source file was auto generated by running 'gradle buildInit --type java-library'
3-
* by 'ilkka' at '12/15/17 10:02 PM' with Gradle 2.8
4+
* Java Snippets code
45
*
5-
* @author ilkka, @date 12/15/17 10:02 PM
66
*/
77
publicclassLibrary {
8-
publicbooleansomeLibraryMethod() {
9-
returntrue;
8+
/**
9+
* Generic 2 array concatenation
10+
* Credits: Joachim Sauer https://stackoverflow.com/questions/80476/how-can-i-concatenate-two-arrays-in-java
11+
* @param first is the first array (not null)
12+
* @param second is the second array (not null)
13+
* @param <T> the element type
14+
* @return concatenated array
15+
*/
16+
publicstatic <T>T[]arrayConcat(T[]first,T[]second) {
17+
T[]result =Arrays.copyOf(first,first.length +second.length);
18+
System.arraycopy(second,0,result,first.length,second.length);
19+
returnresult;
1020
}
1121
}

‎src/test/java/LibraryTest.java‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,24 @@
22
importstaticorg.junit.Assert.*;
33

44
/*
5-
* This Java source file was auto generated by running 'gradle init --type java-library'
6-
* by 'ilkka' at '12/15/17 10:02 PM' with Gradle 2.8
5+
* Java Snippets tests
76
*
8-
* @author ilkka, @date 12/15/17 10:02 PM
97
*/
108
publicclassLibraryTest {
11-
@TestpublicvoidtestSomeLibraryMethod() {
12-
LibraryclassUnderTest =newLibrary();
13-
assertTrue("someLibraryMethod should return 'true'",classUnderTest.someLibraryMethod());
9+
/**
10+
* Tests for {@link Library#arrayConcat(Object[], Object[])}
11+
*/
12+
@Test
13+
publicvoidtestArrayConcat() {
14+
Integer[]integers =Library.arrayConcat(newInteger[5],newInteger[5]);
15+
assertEquals(integers.length,10);
16+
String[]strings =Library.arrayConcat(newString[0],newString[0]);
17+
assertEquals(strings.length,0);
18+
try {
19+
Double[]doubles =Library.arrayConcat(null,null);
20+
fail();
21+
}catch (NullPointerExceptione) {
22+
// expected behaviour, everything is fine
23+
}
1424
}
1525
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp