extension function in java
This commit is contained in:
parent
cd23e7c905
commit
b84b7117a6
@ -0,0 +1,9 @@
|
|||||||
|
@file:JvmName("Lists")
|
||||||
|
package com.baeldung.kotlin
|
||||||
|
|
||||||
|
fun <T> MutableList<T>.swap(firstIndex: Int, secondIndex: Int): MutableList<T> {
|
||||||
|
val tmp = this[firstIndex]
|
||||||
|
this[firstIndex] = this[secondIndex]
|
||||||
|
this[secondIndex] = tmp
|
||||||
|
return this
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
package com.baeldung.kotlin;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static com.baeldung.kotlin.Lists.*;
|
||||||
|
|
||||||
|
public class ListUtilTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldSwapTwoElementsInList() {
|
||||||
|
List<Integer> list = Arrays.asList(0, 1, 2);
|
||||||
|
|
||||||
|
List<Integer> swappedElements = swap(list, 1, 2);
|
||||||
|
|
||||||
|
Assert.assertEquals(swappedElements, Arrays.asList(0, 2, 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user