如何使用IList生成数据库?

问题描述

我有一个根据位置随机列表生成的“数据库”,每个位置都附加了一个随机洲和一个随机类别。

我想将RandomItem更改为NextItem,以便它只选择位置列表中的下一个项目,而不是随机选择它们,但是不幸的是,我不知道要搜索什么来解决这个问题。

我希望在生成一个位置时可以使用(i)的值,但这似乎不起作用。

我将如何更改NextItem来做到这一点?

    public class TestPlacesRepository : PlacesRepository
    {

        const int placeCount = 10;
        readonly List<Location> locations;
        readonly List<Continent> continents;
        readonly List<Category> categories;
        readonly Random random;

        public TestPlacesRepository() : base()
        {
            //Generates the seed for the rng
            this.random = new Random((int)DateTime.Now.Ticks);
            this.locations = new List<Location>();
            this.continents = new List<Continent>();
            this.categories = new List<Category>();

            GenerateLocations();
            GenerateContinents();
            GenerateCategories();

            for (int i = 0; i < placeCount; i++)
                Places.Add(GeneratePlace(i));

        }

        Place GeneratePlace(int number)
        {
            //Creates the x "random" locations from the list of locations below
            Place place = new Place(
                NextItem<Location>(locations),RandomItem<Continent>(continents),RandomItem<Category>(categories),number % 3 == 0);
            return place;
        }

        T NextItem<T>(IList<T> list)
        {
            int index = (int)(i * (list.Count));
            return list[index];
        }

        T RandomItem<T>(IList<T> list)
        {
            int index = (int)(random.NextDouble() * 0.99 * (list.Count));
            return list[index];
        }

        void GenerateLocations()
        {
            //1st 10 places
            locations.Add(new Location("Temples of Angkor","Cambodia"));
            locations.Add(new Location("Great Barrier Reef","Australia"));
            locations.Add(new Location("Machu Picchu","Peru"));
            locations.Add(new Location("Great Wall of China","China"));
            locations.Add(new Location("Taj Mahal","India"));
            locations.Add(new Location("Arizona - Grand Canyon National Park","USA"));
            locations.Add(new Location("Colosseum","Italy"));
            locations.Add(new Location("Iguazu Falls","Brazil/Argentina"));
            locations.Add(new Location("Alhambra","Spain"));
            locations.Add(new Location("Aya Sofya","Turkey"));
         }
        void GenerateContinents()
        {
            //All 7 Continents
            continents.Add(new Continent("Africa"));
            continents.Add(new Continent("Antartica"));
            continents.Add(new Continent("Asia"));
            continents.Add(new Continent("Europe"));
            continents.Add(new Continent("north America"));
            continents.Add(new Continent("Oceania"));
            continents.Add(new Continent("South America"));
        }
        void GenerateCategories()
        {
            //All 7 Categories
            categories.Add(new Category("Activity/Modern Attraction"));
            categories.Add(new Category("district/Square/Town"));
            categories.Add(new Category("Historical Site"));
            categories.Add(new Category("Museum"));
            categories.Add(new Category("Nature/Landscape"));
            categories.Add(new Category("Palace/Castle/Fort/Ruin"));
            categories.Add(new Category("Religion"));
        }

在此处键入

解决方法

仅对每个列表使用索引计数器

int nextLoc = 0;

Location NextLocation()
{
  var loc = locations[nextLoc];
  nextLoc++;
  return loc;
}
,

另一个适用于任何appName.conf(包括IEnumerable)的选项是获取枚举数,然后只需调用IList并为每次迭代返回MoveNext ,例如

Current

注意,您还应该显式调用枚举器。使用完后处置(它实现IDisposible)。例如。在构造函数的末尾添加以下内容:

IEnumerator<T> enumerator = null;

T NextItem<T>(IEnumerable<T> collection)
{
    if (enumerator == null)
    {
        enumerator = collection.GetEnumerator();
    }

    if (!enumerator.MoveNext())
    {
        return null;
    }

    return enumerator.Current;
}

在最新的C#版本中,可以简化为:

if (enumerator != null)
{
    enumerator.Dispose();
}

有一个明显的警告,它仅适用于与之一起使用的第一个集合(因为collection参数仅在首次使用时用于初始化枚举器)。