Как инициализировать переменную равную последнему индексу массива ArrayList? - C#

Узнай цену своей работы

Формулировка задачи:

Всем привет, мне нужно реализовать Enumerator, но перебирать элементы с конца массива ArrayList. Проблема в том, что я не знаю как установить переменной, которая является индексом массива изначально максимальное значение.Воn код моего Enumenator:
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Collections;
  5. namespace ArrayListProject
  6. {
  7. public class PersonEnumerator : IEnumerator
  8. {
  9. int currIndex = person.GetChildrenNumber(); /*Инициализатор поля не может обращаться к нестатическому полю, методу или свойству "PersonEnumerator.person".*/
  10. PersonClass.Person person;
  11. public PersonEnumerator(PersonClass.Person person)
  12. {
  13. this.person = person;
  14. }
  15. #region IEnumerator Members
  16. public object Current
  17. {
  18. get { return person[currIndex]; }
  19. }
  20. public bool MoveNext()
  21. {
  22. currIndex--;
  23. if (currIndex <= -1)
  24. return false;
  25. else
  26. return true;
  27. }
  28. public void Reset()
  29. {
  30. currIndex = person.GetChildrenNumber();
  31. }
  32. #endregion
  33. }
  34. }
Вот код класса Person, в котором находится массив, который необходимо перебрать:
Листинг программы
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using ArrayListProject;
  8. namespace PersonClass
  9. {
  10. public class Person: IPurse
  11. {
  12. public Person(string firstName,string lasName)
  13. {
  14. FirstName = firstName;
  15. LastName = lasName;
  16. }
  17. public string FirstName { get; set; }
  18. public string LastName { get; set; }
  19. #region Дети
  20. ArrayList Children = new ArrayList();
  21. public void AddChild(string firstName, string lastName)
  22. {
  23. Children.Add(new Person(firstName, lastName));
  24. }
  25. public void DeleteChild(int index)
  26. {
  27. Children.RemoveAt(index);
  28. }
  29. public Person GetChild(int index)
  30. {
  31. return (Person)Children[index];
  32. }
  33. #endregion
  34. public int GetChildrenNumber()
  35. {
  36. return Children.Count;
  37. }
  38. public Person this[int index]
  39. {
  40. get { return (Person)Children[index]; }
  41. }
  42. public IEnumerator GetEnumerator()
  43. {
  44. return new PersonEnumerator (this);
  45. }
  46. }
  47. }

Решение задачи: «Как инициализировать переменную равную последнему индексу массива ArrayList?»

textual
Листинг программы
  1. public PersonEnumerator(PersonClass.Person person)
  2.         {
  3.             this.person = person;
  4.             currIndex = this.person.GetChildrenNumber();
  5.         }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

9   голосов , оценка 4.222 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы