将 list<> 的最后 10 个元素放入其他 list<>
Put last 10 elements of list<> into other list<>
我有一个列表<>,里面装满了我的 class 的对象。那个工作正常。
现在我想将最后 10 个元素加载到一个新的 list<>,它是倒序排列的。所以最后一个元素将是新 List<> 的第一个,第 10 个将是新 List<> 的最后一个。
通常这不应该是一项艰巨的任务,但我不知道...它以正确的方式迭代并且在我看来应该可以工作。
Both Lists<> are Lists<lastChanges>. lastChange is my class.
这是我现在得到的:
// x represents the amount of elements for the new List<>
int x = 10;
// elems is the old list. if it contains less than 10 elements,
// x should be the amount instead of 10
if (elems.Count < 10)
{
x = elems.Count;
}
// The start-index is made by elems.Count -> 10 Items -> the 10th item
// would get the index '9'
// as long as i is greater than the amount of the large List<>
// minus the wanted amount for the new List<>
// the loop is going to repeat
for (int i = elems.Count-1; i > elems.Count - x; i--)
{
// lastChanges is my class which both Lists are filled with
lastChanges lastArt = elems[i];
if (lastArt != null)
{
items.Add(lastArt);
}
}
我错过了什么?我真的不认为我还是一个初学者(当然还有很多需要改进的地方)但是我在这里找不到错误...
例如:
元素列表确实包含 2 个元素,
那么 x 将等于 2。
for 循环将开始:
for(int i=1;i>0;i--)
所以循环会 运行 两次。
在第一个 运行 中, 'lastArt' 设置为等于 'elems' 的第二个对象,然后添加到 'items,
在第二个 运行 中,第一个项目将添加到 'items'。
所以,两个项目都添加到 'items',一切都很好。
但为什么我总是出错?
两个对象都定义为 != null...
谢谢!
编辑:
我总是在这一行得到一个 'NullReferenceException':
items.Add(lastArt);
两个对象都定义为 != null,所以在我看来它一定是我的迭代中的一个错误。
尝试使用 LINQ
var result = data.Skip(data.Count - 10).Take(10);
List<SomeType> list = new List<SomeType>(result.Reverse());
循环计数更容易。
与其尝试跟踪 i
从最后一个元素倒数,不如从 1 开始向上计数。
int numElements = 10; // or however you want from the end
for (int i = 1; i <= numElements && i <= elems.Count; i++)
lastItems.Add(elems[elems.Count - i]);
使用 LINQ 更简单。
List<MyClass> lastElements = elems.Reverse().Take(10).ToList();
我有一个列表<>,里面装满了我的 class 的对象。那个工作正常。 现在我想将最后 10 个元素加载到一个新的 list<>,它是倒序排列的。所以最后一个元素将是新 List<> 的第一个,第 10 个将是新 List<> 的最后一个。
通常这不应该是一项艰巨的任务,但我不知道...它以正确的方式迭代并且在我看来应该可以工作。
Both Lists<> are Lists<lastChanges>. lastChange is my class.
这是我现在得到的:
// x represents the amount of elements for the new List<>
int x = 10;
// elems is the old list. if it contains less than 10 elements,
// x should be the amount instead of 10
if (elems.Count < 10)
{
x = elems.Count;
}
// The start-index is made by elems.Count -> 10 Items -> the 10th item
// would get the index '9'
// as long as i is greater than the amount of the large List<>
// minus the wanted amount for the new List<>
// the loop is going to repeat
for (int i = elems.Count-1; i > elems.Count - x; i--)
{
// lastChanges is my class which both Lists are filled with
lastChanges lastArt = elems[i];
if (lastArt != null)
{
items.Add(lastArt);
}
}
我错过了什么?我真的不认为我还是一个初学者(当然还有很多需要改进的地方)但是我在这里找不到错误...
例如:
元素列表确实包含 2 个元素, 那么 x 将等于 2。 for 循环将开始:
for(int i=1;i>0;i--)
所以循环会 运行 两次。
在第一个 运行 中, 'lastArt' 设置为等于 'elems' 的第二个对象,然后添加到 'items,
在第二个 运行 中,第一个项目将添加到 'items'。
所以,两个项目都添加到 'items',一切都很好。
但为什么我总是出错? 两个对象都定义为 != null...
谢谢!
编辑:
我总是在这一行得到一个 'NullReferenceException':
items.Add(lastArt);
两个对象都定义为 != null,所以在我看来它一定是我的迭代中的一个错误。
尝试使用 LINQ
var result = data.Skip(data.Count - 10).Take(10);
List<SomeType> list = new List<SomeType>(result.Reverse());
循环计数更容易。
与其尝试跟踪 i
从最后一个元素倒数,不如从 1 开始向上计数。
int numElements = 10; // or however you want from the end
for (int i = 1; i <= numElements && i <= elems.Count; i++)
lastItems.Add(elems[elems.Count - i]);
使用 LINQ 更简单。
List<MyClass> lastElements = elems.Reverse().Take(10).ToList();