我在c#中有一个对象的通用列表,并希望克隆列表。列表中的项是可克隆的,但似乎没有做list. clone()的选项。
有什么简单的办法吗?
我在c#中有一个对象的通用列表,并希望克隆列表。列表中的项是可克隆的,但似乎没有做list. clone()的选项。
有什么简单的办法吗?
当前回答
如果你的元素是值类型,那么你可以这样做:
List<YourType> newList = new List<YourType>(oldList);
然而,如果它们是引用类型,并且你想要一个深度拷贝(假设你的元素正确地实现了ICloneable),你可以这样做:
List<ICloneable> oldList = new List<ICloneable>();
List<ICloneable> newList = new List<ICloneable>(oldList.Count);
oldList.ForEach((item) =>
{
newList.Add((ICloneable)item.Clone());
});
显然,要替换上面泛型中的ICloneable,并使用实现ICloneable的元素类型进行强制转换。
如果你的元素类型不支持ICloneable,但是有一个复制构造函数,你可以这样做:
List<YourType> oldList = new List<YourType>();
List<YourType> newList = new List<YourType>(oldList.Count);
oldList.ForEach((item)=>
{
newList.Add(new YourType(item));
});
就我个人而言,我会避免使用ICloneable,因为需要保证所有成员的深度副本。相反,我建议使用复制构造函数或类似YourType的工厂方法。CopyFrom(YourType itemToCopy)返回一个新的YourType实例。
这些选项中的任何一个都可以通过方法(扩展或其他方式)包装。
其他回答
如果你只关心值类型……
你知道这种类型:
List<int> newList = new List<int>(oldList);
如果你之前不知道类型,你需要一个helper函数:
List<T> Clone<T>(IEnumerable<T> oldList)
{
return newList = new List<T>(oldList);
}
公正:
List<string> myNewList = Clone(myOldList);
//try this
List<string> ListCopy= new List<string>(OldList);
//or try
List<T> ListCopy=OldList.ToList();
下面的代码应该以最小的更改转移到列表中。
基本上,它的工作原理是在每个连续的循环中插入一个更大范围的新随机数。如果已经存在与它相同或更高的数字,则将这些随机数向上移动1,以便它们转移到新的更大范围的随机索引中。
// Example Usage
int[] indexes = getRandomUniqueIndexArray(selectFrom.Length, toSet.Length);
for(int i = 0; i < toSet.Length; i++)
toSet[i] = selectFrom[indexes[i]];
private int[] getRandomUniqueIndexArray(int length, int count)
{
if(count > length || count < 1 || length < 1)
return new int[0];
int[] toReturn = new int[count];
if(count == length)
{
for(int i = 0; i < toReturn.Length; i++) toReturn[i] = i;
return toReturn;
}
Random r = new Random();
int startPos = count - 1;
for(int i = startPos; i >= 0; i--)
{
int index = r.Next(length - i);
for(int j = startPos; j > i; j--)
if(toReturn[j] >= index)
toReturn[j]++;
toReturn[i] = index;
}
return toReturn;
}
除非需要对List<T>中的每个对象进行实际克隆,否则克隆列表的最佳方法是使用旧列表作为集合参数创建一个新列表。
List<T> myList = ...;
List<T> cloneOfMyList = new List<T>(myList);
对myList的更改,如插入或删除,将不会影响cloneOfMyList,反之亦然。
然而,两个list所包含的实际对象仍然是相同的。
如果你需要一个具有相同容量的克隆列表,你可以尝试这样做:
public static List<T> Clone<T>(this List<T> oldList)
{
var newList = new List<T>(oldList.Capacity);
newList.AddRange(oldList);
return newList;
}