let's say I have a simple List<bool>
. I want to initialize it and add e.g 100 elements to it. To do so, I can do:
var myList = new List<bool>();
for (int i = 0; i < 100; i++)
{
myList.Add(false);
}
but it's not the most elegant approach. Is there any built-in method to simplify it ? I don't want any loops, just for curiosity
List<string> myList = new List<string>() { "one", "two", "three", };
The expression can be any type of object that we want to put on the list. Since we are initializing the list with zeros, our expression will just be 0. The * operator can be used as [object]*n where n is the no of elements in the array.
In C++11 and above, we can use the initializer lists '{...}' to initialize a list. This won't work in C++98 as standard permits list to be initialized by the constructor, not by '{...}' .
To initialize a list in Python assign one with square brackets, initialize with the list() function, create an empty list with multiplication, or use a list comprehension. The most common way to declare a list in Python is to use square brackets.
Using Enumerable.Repeat
var myList = Enumerable.Repeat(false, 100).ToList();
which
Generates a sequence that contains one repeated value.
false
is easy, since it's the default value of boolean:
new List<bool>(new bool[100]);
You could use LINQ and more specifically the Select
and ToList
extension methods:
var myList = Enumerable.Range(1, 100).Select(x => false).ToList();
List<T>
has no specific method to do this. The loop is your best option.
However, you can make it more efficient at runtime, by initializing the list with an initial capacity:
var myList = new List<bool>(100);
When using this constructor, the list will internally allocate an array of 100 elements. If you use the default constructor, it will start of with first 0 and then 4 elements. After 4 items have been added, the list will allocate an array of 8 elements and copy the 4 that were already added over. Then it will grow to 16, 32, 64 and finally 128. All these allocations and copy operations can be avoided by using the constructor with the initial capacity.
Alternatively, if you need to do this in different places in your program, you could make an extension method:
public static void Initialize<T>(this List<T> list, T value, int count)
{
if (list == null)
{
throw new ArgumentNullException("list");
}
if (list.Count != 0)
{
throw new InvalidOperationException("list already initialized");
}
if (list.Capacity < count)
{
list.Capacity = count;
}
for (int i = 0, i < count, i++)
{
list.Add(value);
}
}
You would use it like this:
var myList = new List<bool>();
myList.Initialize(false, 100);
The other option that you have is to use an array.
var myList = new bool[100];
The interesting thing about this specific example is that you do not have to initialize the array. Since false
is the default value for bool
, all elements in the array will automatically have the value false
. If your list does not need to resize dynamically, this is certainly an option to consider.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With