//присваиваем переменной индекс, который равен значению первого элемента var c = linkedList.ElementAt(linkedList.First.Value); //добавляем в лист secondList.AddLast(c); for(int x = 1; x < linkedList.Count; x++ ) { ////присваиваем переменной индекс, который равен значению текущего элемента c = linkedList.ElementAt(c); secondList.AddLast(c); } 

Due to the fact that the index of the elements of the array starts with [0], ArgumentOutOfRangeException crashes (the list always starts with 1)

The question is, is it possible in Sharpe to redefine the first index or solve the problem somehow differently?

Closed due to the fact that the essence of the question is not clear to the participants of the Grundy , PashaPash 9 Aug '16 at 8:55 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • What type of variables is linkedList and secondList? - Grundy
  • 2
    The list always starts with 1 - where does this assumption come from? - Grundy
  • both variables of type LinkedList <int>. Initially, the number of elements of a regular array is entered from the console, then elements are entered there in order and mixed. Then everything is copied to the linkedList. Then the element with value 1 becomes the top of the list. - cruim
  • one
    It is not entirely clear what “solve the problem” means. Are you accessing an index that is not in the container and it crashes? Well, do not apply for such an index. Do you use the index counted from 1, as if it were an index counted from 0, and it crashes? So don't use it wrong, what's the problem? Or do you not know how to turn an index counted from one into an index counted from zero? - VladD
  • @Grundy I force the value of the first element to 1. - cruim

3 answers 3

Since, judging by the description, the values ​​in the list begin with 1, in order to use them as indices in this list, you just need to subtract one when passing the value to ElementAt .

  • ElementAt(k) for a coherent list of O (k), so it would be better not to use it, but walk with foreach or something. - VladD
  • @VladD, and how does foreach help here? Immediately, as I understood, it was just to take an element at an index equal to the current value of the element. That is, in one pass, it will not work anyway, and as I understand the indexer does not have a coherent list - Grundy
  • Oh and true. Now I understand. The TC code tries to emulate a LinkedList using an array and indices (the value + index of the next element is in the array). But since the vehicle does not fully understand what he is doing, he took not an array as the supporting structure, but LinkedList, already ready! Which leads, of course, to a catastrophic performance drop. About foreach you are right, yes. - VladD

And I think the problem here is different. x variable in the loop does not play any role, just the variable c is equal to the first value in the linkedList array and it can be from -max int to max int . Suppose you have linkedList = 10. And the first element linkedList (If it is of type int) is 100, then there is no 100th element in the array, hence ArgumentOutOfRangeException

    The index of elements starts from 0 to linkedList.Count-1 , for example, 10 elements, then 0..9

     for(int x = 0; x < linkedList.Count; x++ ) { c = linkedList.ElementAt(c); secondList.AddLast(c); } 
    • count - 1 why? Just < count - Herrgott
    • @Herrgott for sure. corrected. thank! - Senior Pomidor
    • 3
      in this case, it does not matter from what number x starts; this variable is still not used inside the loop - Grundy
    • @Grundy already noticed :) - Herrgott