BAEL-4387 Add the code inside a test

This commit is contained in:
Sebastian Luna 2020-08-07 22:13:33 -05:00
parent 7f1acd21bf
commit 2679498f32
2 changed files with 32 additions and 33 deletions

View File

@ -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]
}
}

View File

@ -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");
}
}