BAEL-4387 Add the code inside a test
This commit is contained in:
parent
7f1acd21bf
commit
2679498f32
|
@ -1,33 +0,0 @@
|
|||
package com.baeldung.arrayconvertion;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class ArrayToListConversion {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Array.asList()");
|
||||
arrayAsList();
|
||||
System.out.println("\nArrayList<>(Arrays.asList())");
|
||||
independentArray();
|
||||
}
|
||||
|
||||
private static void arrayAsList() {
|
||||
String[] stringArray = new String[] { "A", "B", "C", "D" };
|
||||
List<String> stringList = Arrays.asList(stringArray);
|
||||
System.out.println(stringList); // [A, B, C, D]
|
||||
stringList.set(0, "E");
|
||||
System.out.println(stringList); // [E, B, C, D]
|
||||
System.out.println(Arrays.toString(stringArray)); // [E, B, C, D]
|
||||
}
|
||||
|
||||
private static void independentArray() {
|
||||
String[] stringArray = new String[] { "A", "B", "C", "D" };
|
||||
List<String> stringList = new ArrayList<>(Arrays.asList(stringArray));
|
||||
System.out.println(stringList); // [A, B, C, D]
|
||||
stringList.set(0, "E");
|
||||
System.out.println(stringList); // [E, B, C, D]
|
||||
System.out.println(Arrays.toString(stringArray)); // [A, B, C, D]
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package com.baeldung.arrayconvertion;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class ArrayToListConvertion {
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void givenAnArray_whenConvertToList_returnUnmodifiableList() {
|
||||
String[] stringArray = new String[] { "A", "B", "C", "D" };
|
||||
List<String> stringList = Arrays.asList(stringArray);
|
||||
System.out.println(stringList);
|
||||
stringList.set(0, "E");
|
||||
System.out.println(stringList);
|
||||
System.out.println(Arrays.toString(stringArray));
|
||||
stringList.add("F");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAnArray_whenConvertToList_returnModifiableList() {
|
||||
String[] stringArray = new String[] { "A", "B", "C", "D" };
|
||||
List<String> stringList = new ArrayList<>(Arrays.asList(stringArray));
|
||||
System.out.println(stringList);
|
||||
stringList.set(0, "E");
|
||||
System.out.println(stringList);
|
||||
System.out.println(Arrays.toString(stringArray));
|
||||
stringList.add("F");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue