import java.util.ArrayList;import java.util.List;public class ArrayListExample { public static void main(String[] args) { ArrayList<String> originalList = new ArrayList<>(); originalList.add("Element 1"); originalList.add("Element 2"); originalList.add("Element 3"); ArrayList<String> newList = new ArrayList<>(originalList); // Print the elements of the new list for (String element : newList) { System.out.println(element); } }}In the code above, we create an originalListArrayList and add some elements to it. Then, we create a new ArrayListnewList by passingoriginalList as a parameter to the ArrayList constructor. This constructor initializes the newArrayList with the elements of the originalList.
Finally, we iterate over the elements of thenewList and print them.
Output:
Element 1Element 2Element 3By using this approach, you can create a newArrayList with the same elements as the original one. Note that the elements themselves are not copied, only references to them are stored in the newArrayList. If you modify the elements in the originalList or newList, the changes will be reflected in both lists since they reference the same objects.
Example 2: Create ArrayList from Another ArrayList in Java
- Create anArrayList from another collection using theArrayList(Collection c) constructor.
- Adding all the elements from an existing collection to the newArrayList using theaddAll() method.
importjava.util.ArrayList;importjava.util.List;publicclassCreateArrayListFromCollectionExample {publicstaticvoidmain(String[]args) {List<Integer> firstFivePrimeNumbers=newArrayList<>(); firstFivePrimeNumbers.add(2); firstFivePrimeNumbers.add(3); firstFivePrimeNumbers.add(5); firstFivePrimeNumbers.add(7); firstFivePrimeNumbers.add(11);// Creating an ArrayList from another collectionList<Integer> firstTenPrimeNumbers=newArrayList<>(firstFivePrimeNumbers);List<Integer> nextFivePrimeNumbers=newArrayList<>(); nextFivePrimeNumbers.add(13); nextFivePrimeNumbers.add(17); nextFivePrimeNumbers.add(19); nextFivePrimeNumbers.add(23); nextFivePrimeNumbers.add(29);// Adding an entire collection to an ArrayList firstTenPrimeNumbers.addAll(nextFivePrimeNumbers);System.out.println(firstTenPrimeNumbers); }}
Output
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]Reference
Java ArrayList Source Code Examples
- Sort List of Integers in Ascending and Descending Order Example
- List (ArrayList) Iterator Example
- Create the Immutable ArrayList with List.of() Method Example
- Create Immutable ArrayList with Collections.unmodifiableList() Exmaple
- Java 10 - Copy List into Another List Exmaple
- Java 8 - Copy List into Another List Example
- Java - Copy a List to Another List using Collections.copy() method
- Java - Copy a List to Another List Example
- Java ArrayList spliterator() Method Example
- Java ArrayList sort() Method Example
- Java ArrayList retainAll() Method Example
- Java ArrayList removeIf() Method Example
- Java ArrayList removeAll() Method Example
- Java ArrayList remove() Method Example
- Java ArrayList lastIndexOf() Method Example
- Java ArrayList isEmpty() Method Example
- Java util ArrayList indexOf() Method Example
- Java ArrayList get() Method Example
- Java ArrayList ensureCapacity() Method Example
- Java ArrayList contains() Method Example
- Java ArrayList clone() Method Example
- Java ArrayList clear() Method Example
- Java ArrayList addAll() Method Example
- Java ArrayList add() Method Example
- Java 8 forEach() List Example
- Add Enum Values to ArrayList Example
- Join List Strings with Commas in Java
- Java Stream filter null values example
- Java ArrayList subList() Example
- Get Index of Elements in ArrayList Example
- Java ArrayList removeIf() Example
- Java ArrayList add(), get() and set() Method Example
- Iterate over ArrayList Using forEach Java
- Iterate over ArrayList using Iterator in Java
- Java ArrayList indexOf() and lastIndexOf() Example
- Search an Element in an ArrayList in Java
- Clear ArrayList in Java Example
- Java ArrayList removeAll() Method Example
- Java ArrayList remove() Method Example
- How to Iterate over ArrayList in Java
- How to Remove Element from ArrayList in Java
- How to Access Elements of ArrayList in Java
- Create ArrayList from Another ArrayList in Java
- How to Create an ArrayList and Add Elements to It
Comments
Post a Comment