0

I have a list<>, filled with objects of my class. That one is working fine. Now I want to load the last 10 elements to a new list<>, which is sorted upside-down. So the last Element will be the first of the new List<> and the 10th will be the last one in the new List<>.

Usually this shouldn't be such a hard task but I don't know... it iterates on the right way and should work in my opinion.

Both Lists<> are Lists<lastChanges>. lastChange is my class.

Here's what I got now:

        // 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);
            }              
        }

What am I missing? I really do not think I'm still a beginner (of course much to improve) but I can't find an error here...

For example:

The elems-List does contain 2 Elements, then x would be equal 2. The for-loop would start:

for(int i=1;i>0;i--)

so the loop would run twice.

In the first run there would be 'lastArt' set equal to the second object of 'elems' and than be added to 'items,

in the second run, the first item would be added to 'items'.

So, both items are added to 'items', everything is fine.

But why do I keep getting errors? Both Objects are definetely != null...

Thank you!

edit:

I always get a 'NullReferenceException' in this line:

items.Add(lastArt);

Both Objects are definetely != null, so in my opinion it has to be an error in my iteration.

Marcel
  • 917
  • 3
  • 19
  • 48
  • What is a `List<>` suppose to be? `List`? – Binkan Salaryman Mar 25 '15 at 12:58
  • 1
    What errors do you get? Please create a small example that reproduces the problem. – CodeCaster Mar 25 '15 at 12:59
  • You could do this with Linq `newList = oldList.Reverse().Take(10).ToList();` And add in a `Where(x => x != null)` if needed. – juharr Mar 25 '15 at 13:03
  • @BinkanSalaryman & CodeCaster updated my question. I'll try Linq – Marcel Mar 25 '15 at 13:06
  • 1
    _"I always get a 'NullReferenceException' in this line: `items.Add(lastArt);`"_ - then `items` is `null`. You need to initialize it to `new List`. – CodeCaster Mar 25 '15 at 13:08
  • 1
    In your example where the list contains 2 elements, I don't think this would run twice. According to your code i = (2-1) so 1, and checks this is greater than (2-2) or 0, which it is. So the first loop would run. However, it then reduces i by 1 so it's now 0. The logic would then check if 0 > 0, which it's not, so wouldn't run. – sr28 Mar 25 '15 at 13:12

2 Answers2

4

try to use LINQ

var result = data.Skip(data.Count - 10).Take(10);
List<SomeType> list = new List<SomeType>(result.Reverse());
puko
  • 2,819
  • 4
  • 18
  • 27
1

It's easier to make your loop count up.

Rather than trying to keep track of i counting back from the last element, start with 1 and count up.

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]);

It's even easier to use LINQ.

List<MyClass> lastElements = elems.Reverse().Take(10).ToList();
Oblivious Sage
  • 3,326
  • 6
  • 37
  • 58
  • And the Linq example would fix the null reference exception since it creates a new list anyway. – juharr Mar 25 '15 at 13:11