Skip to content

Commit f0fcb7a

Browse files
committed
Add generic two array concatenation snippet
1 parent c0526fc commit f0fcb7a

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+
public static <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+
import java.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
public class Library {
8-
public boolean someLibraryMethod() {
9-
return true;
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+
public static <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+
return result;
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
import static org.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
public class LibraryTest {
11-
@Test public void testSomeLibraryMethod() {
12-
Library classUnderTest = new Library();
13-
assertTrue("someLibraryMethod should return 'true'", classUnderTest.someLibraryMethod());
9+
/**
10+
* Tests for {@link Library#arrayConcat(Object[], Object[])}
11+
*/
12+
@Test
13+
public void testArrayConcat() {
14+
Integer[] integers = Library.arrayConcat(new Integer[5], new Integer[5]);
15+
assertEquals(integers.length, 10);
16+
String[] strings = Library.arrayConcat(new String[0], new String[0]);
17+
assertEquals(strings.length, 0);
18+
try {
19+
Double[] doubles = Library.arrayConcat(null, null);
20+
fail();
21+
} catch (NullPointerException e) {
22+
// expected behaviour, everything is fine
23+
}
1424
}
1525
}

0 commit comments

Comments
 (0)