簡體   English   中英

如果索引是數組,如何減少列表索引內的數據長度?

[英]How to minus data length inside the index of a list, if the index is an array?

好吧,首先,我不確定我在這個問題中的標題是否表達了我想問的問題。 我只是不確定如何用一句話來描述我的問題,希望標題不會引起任何誤導。

如果我有清單。 列表內包含100個數據: list<100>

如果我將此列表放在1秒鍾的計時器滴答聲中,然后執行以下操作:

myList.RemoveRange(0, 2);

這意味着,列表內部的數據長度每1秒為-2;

這意味着,每1秒將是<98> , <96> , <94> .... <0>

現在我的問題是...我仍然有一個列表,但是列表中將包含一個數組: list<array[100]>

現在,我想要的是,每1秒鍾,列表內數組內部的數據長度將為-2。 但是我不確定該怎么做...

我想要的是每隔1秒<array[98]> , <array[96]> , <array[96]> ... <array[0]>

因此,如果列表包含<array0[100] , array1[100], array2[100]>

如果我將此列表放入循環中,每隔1秒鍾,它應該是

array0[98] , array0[96] ... array0[0]
array1[98] , array1[96] ... array1[0]
array2[98] , array2[96] ... array2[0]

更新:

List<int[]> myList = new List<int[]>();
object myLock = new object();
Random rand = new Random();

public Form1()
{
    timer1second.Start();
}

private void SomeMethod()
{
    int[] myData = new int [100]

    for (int i = 0; i < 100; i++)
    {
        //generate some random number to store inside myData[]
        myData[i] = rand.Next(1 , 10); 
    }

    lock (myLock)
    {
        myList.Add(myData); //mean List[0] = myData[100]
    }
}

private void timer1second_Tick(object sender, EventArgs e)
{
     lock (myLock)
     {
         //do something here in myList to get the myData[100 - 2]
         //so that every 1 second tick, the data length inside the MyData will be -2              
     }
}
  1. Array項轉換為List
  2. 然后從列表中刪除范圍
  3. 將其轉換回Array
  4. 將其重新插入List

這是一個示例:

        int currentIndex = 0;

        var myList = new List<int[]>();
        var intArray = new int[100];
        myList.Add(intArray);

        // Convert to List. 
        var newIntArrayList = myList[currentIndex].ToList();

        // Remove the ranges 
        // Index would be based on you logic
        newIntArrayList.RemoveRange(0, 2);

        //Replace the list with the new arry
        myList[currentIndex] = newIntArrayList.ToArray();

更新: Array.Resize也應該有所幫助。

       int currentIndex = 0;
        int arrayLength = 100;

        var myList = new List<int[]>();
        var intArray = new int[100];
        myList.Add(intArray);

        // Get the array
        var array = myList[currentIndex];

        // Resize
        Array.Resize(ref array, arrayLength-2);

        //Replace the list with the update array
        myList[currentIndex] = array;
List<int> myList = new List<int>();
for (int i = 1; i < 101; i++)
{
    myList.Add(i);
}

for (int i = 100; i > 0; i--)
{
    System.Threading.Threading.Sleep(1000);
    myList.RemoveAt(i);
    i -= 1;
    myList.RemoveAt(i);
}

調整列表和數組的大小是一項昂貴的操作。 您是否會考慮使用方便的界面和優化的基礎結構來滿足您的需求,定制數據結構? 因此,每個刻度都只會增加和代表偏移量的整數值:

class Data
{
    const int Step = 2;

    List<int[]> data;
    List<int> cursors;

    public Data()
    {
        data = new List<int[]>();
    }

    public void AddArray(int[] array)
    {
        data.Add(array);
        cursors.Add(array.Length);
        // or cursors.Add(0), depending on your needs
    }

    public void Tick()
    {
        for (int i = 0; i < cursors.Count; i++)
        {
            cursors[i] -= Step;
            // or cursors[i] += Step, depending on your needs
        }
    }

    public IEnumerable<int> GetValuesAtIndex(int index)
    {
        for (int i = 0, i < data[index].Length; i++)
        {
            if (i > cursors[index]) // or i < cursors[index]
            {
                yield return data[index][i];
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM