检查文本文件中访问的项目
本文关键字:项目 访问 文本 文件 检查 | 更新日期: 2023-09-27 18:15:11
我有一个读取文本文件中的项的代码。它逐行读取它们。当一个项目被读取时,它将被添加到一个列表中,以防止它再次被访问。当列表已满(最大大小)时,它将被清除。但是,有必要检查添加到列表中的项,以防止重新访问该特定项以获得预定义值,即使列表已清除。
请帮我弄清楚如何在c# 2012中做。
namespace SearchTechniques.Algorithms
{
using System;
using System.Collections.Generic;
public abstract class TSBase : SearchTechniquesBase
{
// if Tabu list reaches the size (MaximumTabuListSize), it will be cleared.
private readonly int MaximumTabuListSize = 8;
public TSBase()
{
_tabuList = new List<object>();
}
protected override void RunAlgorithm(List<object> solutions)
{
_solutions = new List<object>();
_tabuList.Clear();
var solution = solutions[0];
solutions.RemoveAt(0);
while (solution != null)
{
_logger.Log("'t" + solution.ToString() + " - considering as next best solution not in tabu list based on cost function'n");
_solutions.Add(solution);
UpdateTabuList(solution);
solution = FindNextBestSolution(solution, solutions);
if (null != solution)
{
solutions.Remove(solution);
}
}
}
// updating tabu list
private void UpdateTabuList(object solution)
{
_tabuList.Add(solution);
if (_tabuList.Count >= MaximumTabuListSize)
{
_logger.Log("clearing tabu list as already reached: " + MaximumTabuListSize.ToString() + "'n");
_tabuList.Clear();
}
}
// finding the next best solution
protected abstract object FindNextBestSolution(object solution, List<object> solutions);
// the _solutions are both the list of current solutions and the tabu list in our case
protected abstract bool SolutionExistsInTabuList(object solution);
protected List<object> _tabuList;
}
}
谢谢
使用List<string>
. contains()方法将帮助确定您是否已经阅读了该条目。如果你想要一个列表,即使你清除了它,你也需要2个列表。