motkot
@motkot
Программирование C#.

Почему метод выдает ошибку?

Нужно,чтобы метод ArrayDiff удалял все такие же элементы из a,то есть если a = 1,2,2,1 и b = 1, то результат = 2,2
Ошибка:
ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Kata.ArrayDiff (System.Int32[] a, System.Int32[] b) (at Assets/Test.cs:16)
Test.Start () (at Assets/Test.cs:27)
Код:
using System.Collections.Generic;
using UnityEngine;

public static class Kata
{

    public static int[] ArrayDiff(int[] a, int[] b)
    {
        List<int> result = new List<int>();
        for(int BNumber = 0; BNumber != b.Length; BNumber++)
        {
            for(int ANumber = 0; ANumber != a.Length; ANumber++)
            {
                if (a[ANumber] != b[BNumber])
                {
                    result.RemoveAt(ANumber);
                }
            }
        }
        return result.ToArray();
    }
}
public class Test : MonoBehaviour
{
    void Start()
    {
        int[] array = Kata.ArrayDiff(new int[] { 2, 2, 1, 2 }, new int[] { 2 });
        foreach(int i in array)
        {
            Debug.Log( array[i] );
        }
    }
}
  • Вопрос задан
  • 180 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы