更新列表<class> </class>中的列表

时间:2015-04-21 07:54:43

标签: c# list

由于有list<list<>>是不好的做法,我创建了一个包含2个列表的类:

public class TouchSet
{
    public List<DateTime> timeList = new List<DateTime>(ammountOfXValues);
    public List<int> touchList = new List<int>(ammountOfXValues);
}

然后我有一个用于初始化整个事物的功能,所以我可以在未来的路上使用它:

public void initializeTouchDataListObject()
{
    touchSetList = new List<DataStructure.TouchSet>(DataStructure.maxButtonsActive);

    List<int> tempTouchList = new List<int>();
    List<DateTime> tempTimeList = new List<DateTime>();
    for (int a = 0; a < DataStructure.maxButtonsActive; a++)
    {
        DataStructure.TouchSet tempTouchSet = new DataStructure.TouchSet();
        tempTouchSet.timeList = tempTimeList;
        tempTouchSet.touchList = tempTouchList;
        touchSetList.Add(tempTouchSet);
    }
}

这是我在列表中添加值的循环:

for (int i = 0; i < DataStructure.maxButtonsActive; i++)
{
    if(touchSetList[i].timeList.Count == DataStructure.ammountOfXValues)
    {
        //RemoveAt removes at the given index within a list
        touchSetList[i].timeList.RemoveAt(0);
        touchSetList[i].touchList.RemoveAt(0);
        //add
        touchSetList[i].timeList.Add(DateTime.Now);
        touchSetList[i].touchList.Add(temp);
    }
    else if(touchSetList[i].timeList.Count < DataStructure.ammountOfXValues)
    {
        //add
        touchSetList[i].timeList.Add(DateTime.Now);
        touchSetList[i].touchList.Add(temp);
    }
    else
    {
        int overLength = touchSetList[i].timeList.Count - DataStructure.ammountOfXValues;
        //remove
        touchSetList[i].timeList.RemoveRange(0, overLength + 1);
        touchSetList[i].touchList.RemoveRange(0, overLength + 1);

        //add
        touchSetList[i].timeList.Add(DateTime.Now);
        touchSetList[i].touchList.Add(temp);
    }
}

我面临的问题是,在一次通过for循环的过程中,它会为每个touchList添加临时值,而不仅仅是touchSetList[i]的触摸列表。

例如,在向touchSetList[i].touchList添加temp之后,每隔一个touchList也包含temp,而不仅仅是索引i应用于列表中的类的那个。

我不确定为什么List会以这种方式运行,以及为什么它将值添加到每个列表而不仅仅是具有相应索引的列表。我的印象是你可以使用索引来访问列表中的单个项目。任何指针或建议都表示赞赏。

4 个答案:

答案 0 :(得分:4)

public void initializeTouchDataListObject()
{
    touchSetList = new List<DataStructure.TouchSet>(DataStructure.maxButtonsActive);

    for (int a = 0; a < DataStructure.maxButtonsActive; a++)
    {
        List<DateTime> tempTimeList = new List<DateTime>();
        List<int> tempTouchList = new List<int>();
        DataStructure.TouchSet tempTouchSet = new DataStructure.TouchSet();
        tempTouchSet.timeList = tempTimeList;
        tempTouchSet.touchList = tempTouchList;
        touchSetList.Add(tempTouchSet);
    }
}

您不会为每个新的tempTouchSet创建一个新的tempTimeList和tempTouchList,因此它们都会被传递到指向同一列表的指针。

在循环中对tempTimeList和tempTouchList进行初始化,并为每个tempTouchSet获取一个新的。

答案 1 :(得分:2)

实际上我会重新设计整个事情。我从您的代码中读到的是,您希望存储触摸事件的时间戳以及有关触摸事件的一些信息。

所以我设计了一个包含所有数据的类:

public class TouchInfo
{
    public DateTime touchTime;
    public int touchEvent;
}

然后,您可以轻松存储一个触摸事件列表,而不必保持两个列表同步。

List<TouchInfo> touchEvents = new List<TouchInfo>();

public void initializeTouchDataListObject()
{
    for (int a = 0; a < DataStructure.maxButtonsActive; a++)
    {
        touchEvents.add(new TouchInfo());
    }
}

答案 2 :(得分:0)

initializeTouchDataListObject()方法中,您可以创建tempTouchListtempTimeList。然后,对于每个TouchSet,您可以将这些列表添加为timeListtouchList。这里的问题是两个创建的列表由REFERENCE传递,这意味着每个TouchSet都引用了两个完全相同的列表。它们共享相同的列表,因此在每次迭代中每个TouchSet都会更改列表。

答案 3 :(得分:0)

你有3个条件。添加(temp)在所有3中。

&#13;
&#13;
1) a < b
2) a = b
3) a > c
​
&#13;
&#13;
&#13;

相关问题