Add a range of elements to a list at a given index, replacing the its contents from that index in the list and resizing it (if need it)

VansFannel

I'm developing a WinForm application with C# and .NET Framework 4.7.1.

I'm doing this because there is always enough room in offsprint1 to add it crossoverParent2:

private void Crossover(
    List<byte> offsprint1,
    List<byte> crossoverParent2,
    int crossoverPoint)
{
    for (int index = crossoverPoint, i = 0; index < offsprint1.Count; index++, i++)
        offsprint1[index] = crossoverParent2[i];
}

But now, I'm going to change it and sometime there won't be enough room in offsprint1. With the previous code, I will get an IndexOutOfRangeException in offsprint1[index].

I've thought to use List.AddRange, but there isn't a version to set the index (crossoverPoint) where I want to start replacing the content of offsprint1 with the contents of crossoverParent2.

I want to replace the contents of the list from a given index (crossoverPoint), and resize the list if there is not enough room to store the contents of crossoverParent2. In other words:

(offspring1.Count - crossoverPoint) < crossoverParent2.Count

How can I do that?

Gilad Green

Instead of changing the given list I'd recommend returning a new list with the desired result. In this case - all items from first list until given index, replaced items from second list and eventually any other items from first list after those replaced ones.

You can do it like this:

offsprint1.Take(crossoverPoint) // Original items
          .Concat(crossoverParent2) // Replaced items
          .Concat(offsprint1.Skip(crossoverPoint + crossoverParent2.Count) // Rest of original items

As a side note as you wrote: "I've thought to use List.AddRange, but there isn't a version to set the index" then there exists a method List<T>.InsertRange:

Inserts the elements of a collection into the List at the specified index.

As for your question from the comments. Simplest thing is to try it out:

List<int> data = new List<int> { 1,2,3,4,5};
data.InsertRange(2,new List<int> {6,7,8});

Console.WriteLine(string.Join(", ",data)); // 1, 2, 6, 7, 8, 3, 4, 5

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Accessing NumPy array elements not in a given index list

Divide elements of a list by integer with list comprehension: index out of range

Get index range of the repetitive elements in the list

Delete elements from python array with given index of elements as list

Image resizing, IndexError: list assignment index out of range

Extract same index elements from a list

Add a number to list in a specified index range

BeautifulSoup list index out of range error with multiple .contents

Index Out of Range in a List

Python | How to group value to different lists from a main list given a certain range of number for each index

python: Finding a index in one list and then replacing second list with the item from a index in first list

assign object from the list using its index

How to add multiple elements in a list together by index?

List index out of range

List index out of range

List - Index Out Of Range

Maximum value from a list of lists and its index

return closest item to a given value in a list and its index

Swapping elements based on index given from another list

List index out of range

List index out of range

IndexError: list index out of range when checking matrix contents

Join string from list of string on range of index

accessing specific elements of dictionary given a list of index

Need to add to some list elements from a range of values

How to add index range of one string list to another string list

finding the index of the elements appended to new list from its old list - python 2

Replacing certain elements of an array based on a given index

List index out of range: but it is not