This tutorial will discuss methods to add one list’s elements at the end of another list in C#. Add a List to Another List With the List.AddRange() Function in C#The easiest method for appending one list’s elements at the end of the other list is to use the List.AddRange() method in C#. The List.AddRange(x) method adds the elements of the collection x in the list. The following code example shows us how to add one list to another list with the List.AddRange() function in C#. using System;
using System.Collections.Generic;
namespace add_list
{
static void Main(string[] args)
{
List<string> first = new List<string> { "do", "rey", "me" };
List<string> second = new List<string> { "fa", "so", "la", "te" };
first.AddRange(second);
foreach(var e in first)
{
Console.WriteLine(e);
}
}
}
}
Output: do
rey
me
fa
so
la
te
We created and initialized 2 lists of strings, first and second, in the above code. We appended the second list elements at the end of the first list with the first.AddRange(second) function. In the end, we displayed the elements of the first list. DelftStack articles are written by software geeks like you. If you also would like to contribute to DelftStack by writing paid articles, you can check the write for us page. Related Article - Csharp ListGet the Last Element of a List in C#Iterate Through a List in C# Since list is an interface, one can’t directly instantiate it. However, one can create objects of those classes which have implemented this interface and instantiate them.
Few classes which have implemented the List interface are Stack, ArrayList, LinkedList, Vector etc.
Syntax:
List<Integer> list=new ArrayList<Integer>();
List<Integer> llist=new LinkedList<Integer>();
List<Integer> stack=new Stack<Integer>();
Examples:
import java.util.*;
import java.util.function.Supplier;
public class GFG {
public static void main(String args[])
{
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(3);
System.out.println("ArrayList : " + list.toString());
List<Integer> llist = new LinkedList<Integer>();
llist.add(2);
llist.add(4);
System.out.println("LinkedList : " + llist.toString());
List<Integer> stack = new Stack<Integer>();
stack.add(3);
stack.add(1);
System.out.println("Stack : " + stack.toString());
}
}
|
Output:
ArrayList : [1, 3]
LinkedList : [2, 4]
Stack : [3, 1]
Double Brace Initialization can also be used to do the above work.
Syntax:
List<Integer> list=new ArrayList<Integer>(){{
add(1);
add(2);
add(3);
}};
Examples:
import java.util.*;
public class GFG {
public static void main(String args[])
{
List<Integer> list = new ArrayList<Integer>() {{
add(1);
add(3);
} };
System.out.println("ArrayList : " + list.toString());
List<Integer> llist = new LinkedList<Integer>() {{
add(2);
add(4);
} };
System.out.println("LinkedList : " + llist.toString());
List<Integer> stack = new Stack<Integer>() {{
add(3);
add(1);
} };
System.out.println("Stack : " + stack.toString());
}
}
|
Output:
ArrayList : [1, 3]
LinkedList : [2, 4]
Stack : [3, 1]
-
Creating Immutable List
Arrays.asList() creates an immutable list from an array. Hence it can be used to instantiate a list with an array.
Syntax:
List<Integer> list=Arrays.asList(1, 2, 3);
Examples:
import java.util.Arrays;
import java.util.List;
public class GFG {
public static void main(String args[])
{
List<Integer> list = Arrays.asList(1, 2, 3);
System.out.println("List : " + list.toString());
}
}
|
-
Creating Mutable List
Syntax:
List<Integer> list=new ArrayList<>(Arrays.asList(1, 2, 3));
Examples:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class GFG {
public static void main(String args[])
{
List<Integer> list = new ArrayList<>(
Arrays.asList(1, 2, 3));
System.out.println("List : " + list.toString());
list.add(5);
System.out.println("Modified list : " + list.toString());
}
}
|
Output:
List : [1, 2, 3]
Modified list : [1, 2, 3, 5]
There are various methods in Collections class that can be used to instantiate a list. They are:
-
Collections class has a static method addAll() which can be used to initialize a list. Collections.addAll() take in any number of elements after it is specified with the Collection in which the elements are to be inserted.
Syntax:
List<Integer> list = Collections.EMPTY_LIST;
Collections.addAll(list = new ArrayList<Integer>(), 1, 2, 3, 4);
Examples:
import java.util.*;
public class GFG {
public static void main(String args[])
{
List<Integer> list = new ArrayList<Integer>();
Collections.addAll(list, 1, 2, 3, 4);
System.out.println("List : " + list.toString());
}
}
|
Output:
List : [1, 2, 3, 4]
-
Collections.unmodifiableList() returns a list which can’t be altered i.e. it can neither add or delete an element. Any attempt to modify the list will result in an UnsupportedOperationExample.
Syntax:
List<Integer> list = Collections
.unmodifiableList(Arrays.asList(1, 2, 3));
Example 1:
import java.util.*;
public class GFG {
public static void main(String args[])
{
List<Integer> list = Collections.unmodifiableList(
Arrays.asList(1, 2, 3));
System.out.println("List : " + list.toString());
}
}
|
Example 2:
import java.util.*;
public class GFG {
public static void main(String args[])
{
try {
List<Integer> list = Collections.unmodifiableList(
Arrays.asList(1, 2, 3));
System.out.println("List : " + list.toString());
System.out.println("Trying to modify the list");
list.set(0, list.get(0));
}
catch (Exception e) {
System.out.println("Exception : " + e);
}
}
}
|
Output:
List : [1, 2, 3]
Trying to modify the list
Exception : java.lang.UnsupportedOperationException
-
Collections.singletonList() returns an immutable list consisting of one element only.
Syntax:
List<Integer> list = Collections.singletonList(2);
Example 1:
import java.util.*;
public class GFG {
public static void main(String args[])
{
List<Integer> list = Collections.singletonList(2);
System.out.println("List : " + list.toString());
}
}
|
With the introduction of Stream and functional programming in Java 8, now one can construct any stream of objects and then collect them as a list.
Syntax:
1. List<Integer> list
= Stream.of(1, 2, 3)
.collect(Collectors.toList());
2. List<Integer> list
= Stream.of(1, 2, 3)
.collect(Collectors.toCollection(ArrayList::new));
3. List<Integer> list
= Stream.of(1, 2, 3, 4)
.collect(Collectors.collectingAndThen(Collectors.toList(),
Collections::unmodifiableList));
Examples:
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class GFG {
public static void main(String args[])
{
List<Integer> list1 = Stream.of(1, 2, 3)
.collect(Collectors.toList());
System.out.println("List using Syntax 1: "
+ list1.toString());
List<Integer> list2 = Stream
.of(3, 2, 1)
.collect(
Collectors
.toCollection(ArrayList::new));
System.out.println("List using Syntax 2: "
+ list2.toString());
List<Integer> list3 = Stream
.of(1, 2, 3, 4)
.collect(
Collectors
.collectingAndThen(
Collectors.toList(),
Collections::unmodifiableList));
System.out.println("List using Syntax 3: "
+ list3.toString());
}
}
|
Output:
List using Syntax 1: [1, 2, 3]
List using Syntax 2: [3, 2, 1]
List using Syntax 3: [1, 2, 3, 4]
Java 9 introduced List.of() method which takes in any number of arguments and constructs a compact and unmodifiable list out of them.
Syntax:
List<Integer> unmodifiableList = List.of(1, 2, 3);
Examples:
import java.util.List;
public class GFG {
public static void main(String args[])
{
List<Integer> unmodifiableList = List.of(1, 2, 3);
System.out.println("List : "
+ unmodifiableList.toString());
}
}
|
OUTPUT:
[1, 2, 3]
|