Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to overwrite list?

Tags:

c#

list

I need collection that is similar to list, instead of always adding items when particular length is reached it should start overwriting values from the first index and continue in circular order.

I.e. for 4 items limit:

specialList.Add(100); // {100}
specialList.Add(101); // {100, 101}
specialList.Add(102); // {100, 101, 102}
specialList.Add(103); // {100, 101, 102, 103}
specialList.Add(104); // {104, 101, 102, 103}
specialList.Add(105); // {104, 105, 102, 103}
like image 546
John Avatar asked Jan 15 '17 04:01

John


People also ask

How do you overwrite a list in Python?

Python Replace Item in List: Using List Indexing. The easiest way to replace an item in a list is to use the Python indexing syntax . Indexing allows you to choose an element or range of elements in a list. With the assignment operator, you can change a value at a given position in a list.

How do I update a list?

You can update single or multiple elements of lists by giving the slice on the left-hand side of the assignment operator, and you can add to elements in a list with the append() method.

How do you overwrite a list in C#?

Usage is easy as: var list = new ListCycle<int>(10); //fill the list for (int i = 0; i < 10; i++) { list. Add(i); } //now list is: // 0, 1, 2, 3, ... //add more items will start from first list. Add(100); //overrides first item list.

How do you replace multiple elements in a list in Python?

One way that we can do this is by using a for loop. One of the key attributes of Python lists is that they can contain duplicate values. Because of this, we can loop over each item in the list and check its value. If the value is one we want to replace, then we replace it.


1 Answers

(Updated to show a General list class) This is a class which could be used for special list class that loops (cycles to the first item) when reached to last element:

public class ListCycle<T> : IList<T>
{

    int curIndex = -1;
    List<T> list;
    int nMax;

    public ListCycle(int n)
    {
        list = new List<T>(n);
        nMax = n;
    }

    /// <summary>returns the current index we are in the list</summary>
    public int CurIndex { get { return curIndex; } }

    public int IndexOf(T item) { return list.IndexOf(item); }
    public bool Contains(T item) { return list.Contains(item); }
    public int Count { get { return list.Count; } }
    public bool IsReadOnly { get { return false; } }
    public IEnumerator<T> GetEnumerator() { return list.GetEnumerator(); }
    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return list.GetEnumerator(); }

    public T this[int index]
    {
        get { return list[index]; }
        set { list[index] = value; }
    }

    public void Add(T item)
    {
        curIndex++; if (curIndex >= nMax) curIndex = 0;
        if (curIndex < list.Count)
            list[curIndex] = item;
        else
            list.Add(item);
    }

    public void Clear()
    {
        list.Clear();
        curIndex = -1;
    }

    //other mehods/properties for IList ...
    public void Insert(int index, T item) { throw new NotImplementedException(); }
    public bool Remove(T item) { throw new NotImplementedException(); }
    public void RemoveAt(int index) { throw new NotImplementedException(); }
    public void CopyTo(T[] array, int arrayIndex) { throw new NotImplementedException(); }

}

Usage is easy as:

var list = new ListCycle<int>(10);

//fill the list
for (int i = 0; i < 10; i++)
{
    list.Add(i);
}

//now list is:
// 0, 1, 2, 3, ...

//add more items will start from first
list.Add(100); //overrides first item
list.Add(101); //overrides second item

//now list is:
// 100, 101, 2, 3, ...
like image 154
S.Serpooshan Avatar answered Sep 20 '22 05:09

S.Serpooshan