在C#中修改对象

Bre*_*ata 2 c#

(如果你能想到一个更好的头衔,请告诉我.)

我正在研究路线优化计划.我从一个路线需要包含的点列表开始.我的第一步是创建所有可能路由(排列)的列表.然后我删除任何我可以的路线(例如,如果一个停止必须在另一个之前).完成后,我计算每个可能路线中每个点之间的距离和时间.每个点都是一个对象(TPoint),所有距离和时间值都存储在一个名为TData的单独类中,该类存储在每个TPoint实例中.我的问题是这样的:当我尝试更新TData时,比如第一站,在第一个可能的路线中,它将更新每个可能路线中相同TPoint的TData.这是因为类是引用类型并存储在堆上.我正在寻找一种解决方案,允许我在每个TPoint上存储TData.

下面是一些示例代码(下面的代码演示了当我修改一个对象(TPoint)时,我实际上只是使用引用来修改堆上的对象):

主要

// Let's create a list of points we need to hit.
List<TPoint> lstInitial = new List<TPoint>();
lstInitial.Add(new TPoint("A", new TData(-1, -1)));
lstInitial.Add(new TPoint("B", new TData(-1, -1)));
lstInitial.Add(new TPoint("C", new TData(-1, -1)));

// Now let's get all possible routes
IList<IList<TPoint>> lstPermutations = Permutations(lstInitial);

// Let's write these values to the first point, in the first possible route.
lstPermutations[0][0].oTData.distance = 10;
lstPermutations[0][0].oTData.minutes = 20;

foreach (IList<TPoint> perm in lstPermutations)
{
    foreach (TPoint p in perm)
    {
        Response.Write(p.id + "|" + p.oTData.distance + "|" + p.oTData.minutes);
        Response.Write(" ");
    }
    Response.Write("<br />");
}
Run Code Online (Sandbox Code Playgroud)

置换函数

// Get permutations
private static IList<IList<T>> Permutations<T>(IList<T> list)
{
    List<IList<T>> perms = new List<IList<T>>();

    // If the list is empty, return an empty list.
    if (list.Count == 0)
    {
        return perms;
    }

    // This is a loop method to get the factorial of an integer
    int factorial = 1;
    for (int i = 2; i <= list.Count; i++)
    {
        // shortcut for: factorial = factorial * i;
        factorial *= i;
    }

    for (int v = 0; v < factorial; v++)
    {
        //List<T> s = new List<T>(list);
        List<T> s = new List<T>(list);

        int k = v;
        for (int j = 2; j <= list.Count; j++)
        {
            int other = (k % j);
            T temp = s[j - 1];
            s[j - 1] = s[other];
            s[other] = temp;

            k = k / j;
        }
        perms.Add(s);
    }

    return perms;
}
Run Code Online (Sandbox Code Playgroud)

类

public class TPoint
{
    public TPoint(string _id, TData _oTData)
    {
        id = _id;
        oTData = _oTData;
    }

    public string id { get; set; }
    public int someInt { get; set; }
    public TData oTData { get; set; }
}

public class TData
{
    public TData(int _distance, int _minutes)
    {
        distance = _distance;
        minutes = _minutes;
    }

    public int distance { get; set; }
    public int minutes { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

看起来好像我已经设法把自己画成一个角落.我可以想到一些解决方案,但它们看起来很混乱所以我想我会问这个问题的专家.

编辑

任何人都可以想到为什么这不是一个好主意?

而不是这个,它修改堆上的对象(并影响每个可能路径中的每个点):

lstPermutations[0][0].oTData.distance = 10;
lstPermutations[0][0].oTData.minutes = 20;
Run Code Online (Sandbox Code Playgroud)

使用它,它只是创建一个新的类实例:

TPoint oTPoint = new TPoint(lstPermutations[0][0].id, new TData(10, 20));
lstPermutations[0][0] = oTPoint;
Run Code Online (Sandbox Code Playgroud)

Jea*_*rin 5

如果你使TData成为一个结构,那么它将按值复制而不是通过引用复制.否则你将不得不制作一个复制值的浅层克隆.

  • 这是*I*喜欢C#比Java更好的原因之一.您无法在Java中定义新值类型. (2认同)