From 7f1acd21bfa9932ca7a09d35d618b294ba112fdb Mon Sep 17 00:00:00 2001 From: Sebastian Luna Date: Tue, 21 Jul 2020 21:30:17 -0500 Subject: [PATCH] BAEL-4387 Add files to handle Array to ArrayList convertion --- .../ArrayToListConversion.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 java-collections-conversions-2/src/main/java/com/baeldung/arrayconvertion/ArrayToListConversion.java diff --git a/java-collections-conversions-2/src/main/java/com/baeldung/arrayconvertion/ArrayToListConversion.java b/java-collections-conversions-2/src/main/java/com/baeldung/arrayconvertion/ArrayToListConversion.java new file mode 100644 index 0000000000..7e1002b22c --- /dev/null +++ b/java-collections-conversions-2/src/main/java/com/baeldung/arrayconvertion/ArrayToListConversion.java @@ -0,0 +1,33 @@ +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 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 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] + } +} \ No newline at end of file