queuetoarray-80

问题描述:c#中的Queue和stack怎么遍历?直接使用ToArray()去获取嘛? 大家好,小编为大家解答一个有趣的事情的问题。很多人还不知道一个有趣的事情,现在让我们一起来看看吧!

蚁群算法求解最短路的C#代码

queuetoarray-80的相关图片

Queue和Stack绝大多数情况是不需要遍历的,因为它们设计出来不是为了这个功能。

每种泛型集合都有自己使用的场景。

如果要遍历 用foreach或者linq都可以 不需要先ToArray。

C# 队列移除某个元素的相关图片

C# 队列移除某个元素

你好,希望对你有所帮助

using System;

using System.Collections.Generic;。

using System.Text;。

namespace AntSystem。

public class AA。

{

/**//// <summary>。

/// 对信息量的重视程度。

/// </summary>。

private int alpha;。

/**//// <summary>。

/// 启发式信息的受重视程度。

/// </summary>。

private int beta;。

/**//// <summary>。

/// 信息素的挥发速度。

/// </summary>。

private double lo;。

/**//// <summary>。

/// 城市距离矩阵。

/// </summary>。

private double[,] City;。

/**//// <summary>。

/// 信息素矩阵。

/// </summary>。

private double[,] Message;。

/**//// <summary>。

/// opneList用于存放下一步可行城市。

/// </summary>。

private Queue<int> openList=new Queue<int> ();。

/**//// <summary>。

/// closedList用于存放已经访问过的城市。

/// </summary>。

private Queue<int> closedList=new Queue<int> ();。

/**//// <summary>。

/// 储存较好的路径。

/// </summary>。

private Queue <int> BestList=new Queue<int> ();。

private int Pro_time = 0;。

/**//////////////////////////////////////////////////////////。

/// <summary>。

/// 构造函数:形成城市距离和信息素矩阵。

/// </summary>。

/// <param name="city">城市距离矩阵</param>。

/// <param name="Lo"> 信息素的挥发速度</param>。

public AA(double[,] city,double Lo,int Alpha,int Beta)。

{

alpha = Alpha;。

beta = Beta;。

lo=Lo;。

int temp = Convert.ToInt32( Math.Sqrt(city.Length));。

City=new double [temp,temp];。

Message=new double [temp,temp];。

for (int i = 0; i < temp; i++)。

{

for (int j = 0; j < temp; j++)。

{。

City[i, j] = city[i, j];。

}。

}

//初始化信息素矩阵。

for (int i = 0; i < temp; i++)。

{

for (int j = 0; j < temp; j++)。

{。

if (i != j)。

{。

Message[i, j] = (double)1 / (temp * temp - temp);。

}。

}。

}

}

/**/////////////////////////////////////////////////////////////。

/// <summary>。

/// 改变信息素矩阵,closed_list为较好的路径。

/// </summary>。

/// <param name="closed_list"></param>。

private void Change_Message(Queue<int> closed_list)。

{

lock (this)。

{

int[] temp_Array = new int[closed_list.Count];。

temp_Array = closed_list.ToArray();。

for (int i = 0; i < closed_list.Count - 1; i++)。

{。

Message[temp_Array[i], temp_Array[i + 1]] = Message[temp_Array[i], temp_Array[i + 1]] + lo / ((1 - lo) *Convert.ToInt32(Get_Weight(closed_list)+1));。

}。

Message[temp_Array[temp_Array.Length - 1], temp_Array[0]] = Message[temp_Array[temp_Array.Length - 1], temp_Array[0]] + lo / ((1 - lo) *Convert.ToInt32(Get_Weight(closed_list)));。

for (int i = 0; i < closed_list.Count; i++)。

{。

for (int j = 0; j < closed_list.Count; j++)。

{。

Message[i, j] = (1 - lo) * Message[i, j];。

}。

}。

}

}

/**////////////////////////////////////////////////////////////////。

/// <summary>。

/// 输入一个链表,计算出其对应的总路径。

/// </summary>。

/// <param name="closed_list"></param>。

/// <returns></returns>。

public double Get_Weight(Queue <int> closed_list)。

{

lock (this)。

{

double sum = 0;。

int[] temp_Array = new int[closed_list.Count];。

temp_Array = closed_list.ToArray();。

for (int i = 0; i < Convert.ToInt32(temp_Array.Length) - 1; i++)。

{。

sum = sum + City[temp_Array[i], temp_Array[i + 1]];。

}。

sum = sum + City[temp_Array[temp_Array.Length - 1], temp_Array[0]];。

return sum;。

}

}

/**///////////////////////////////////////////////////////////////。

/// <summary>。

/// 产生到i城市后,下一个可走城市的集合。并将城市编号加入到openList中。

/// 产生的城市不可以已经存在closedList中。

/// </summary>。

/// <param name="i"></param>。

private void NextCity()。

{

openList.Clear();。

int temp_int=Convert.ToInt32(Math.Sqrt(City.Length));。

for (int i = 0; i < temp_int; i++)。

{

if (closedList.Contains(i) ==false)。

{。

openList.Enqueue(i);。

}。

}

}

/**///////////////////////////////////////////////////////////////。

/// <summary>。

/// 选择应该走那条路,选择完路A后,清空openList,再把A加入到openList中。

/// </summary>。

/// <returns></returns>。

private int choiceRoute()。

{

int index = 0;//记录选择的城市。

Random random = new Random();。

double random_value =(double) random.NextDouble();//随机选择的概率。

int[] temp_Array=new int [openList.Count];。

temp_Array=openList.ToArray();。

double sum_Message = 0;//openList所有节点的总信息量。

for (int i = 0; i < openList.Count; i++)。

{

double eta = 1 / City[Pro_time, temp_Array[i]];。

sum_Message = sum_Message +Math.Pow(Message[Pro_time, temp_Array[i]],alpha)*Math.Pow (eta,beta);。

}

double temp=0;。

for (int j = 0; j < openList.Count; j++)。

{

double eta = 1 / City[Pro_time, temp_Array[j]];。

temp=temp+Math.Pow(Message[Pro_time,temp_Array[j]],alpha)*Math.Pow(eta,beta)/sum_Message;。

if (temp > random_value)。

{。

index = temp_Array [j];。

break;。

}。

}

openList.Clear();。

openList.Enqueue(index);。

return index;。

}

/**//////////////////////////////////////////////////////////////。

public Queue<int> Main_DW()。

{

BestList.Clear();。

/**////共循环20次。

for (int i = 0; i < 4; i++)。

{

/**////共有n只蚂蚁n=City'number Convert.ToInt32(Math.Sqrt(City.Length))。

for (int j = 0; j < Convert.ToInt32(Math.Sqrt(City.Length)); j++)。

{。

openList.Enqueue(0);。

closedList.Clear();。

while (openList.Count != 0 && closedList.Count != Convert.ToInt32(Math.Sqrt(City.Length)))。

{。

int temp = openList.Dequeue();。

Pro_time = temp;。

closedList.Enqueue(temp);。

if (openList.Count == 0 && closedList.Count == Convert.ToInt32(Math.Sqrt(City.Length)))。

{。

if (BestList.Count == 0)。

{。

int[] temp_Array = new int[Convert.ToInt32(Math.Sqrt(City.Length))];。

temp_Array = closedList.ToArray();。

for (int k = 0; k < Convert.ToInt32(Math.Sqrt(City.Length)); k++)。

{。

BestList.Enqueue(temp_Array[k]);。

}。

}。

if (Get_Weight(BestList) > Get_Weight(closedList))。

{。

BestList.Clear();。

int[] temp_Array = new int[Convert.ToInt32(Math.Sqrt(City.Length))];。

temp_Array = closedList.ToArray();。

for (int k = 0; k < Convert.ToInt32(Math.Sqrt(City.Length)); k++)。

{。

BestList.Enqueue(temp_Array[k]);。

}。

}。

}。

NextCity();。

choiceRoute();。

}。

}。

Change_Message(BestList);//修改信息量。

}

return BestList;。

}

}

用c#把一串数字的连续的和不连续的区分开的相关图片

用c#把一串数字的连续的和不连续的区分开

队列只能移除第一个对象,如果想把某个对象移除,它之前的所有对象都得移除。

方法:Dequeue()

如果想移除某个元素,请把队列转换为List。

List l = new List(q.ToArray())。

c#中如何读取文本文件的最后一行?的相关图片

c#中如何读取文本文件的最后一行?

         public static string QueueString(string input){。

            var arr = new System.Collections.ArrayList();。

            var lines = input.Split(' ',',',',');。

            int min,max,tmp = 1;。

            max = min = int.Parse(lines[0]);。

            foreach (var line in lines) {。

                tmp = int.Parse(line);。

                if ((tmp - max)>1) {。

                    arr.Add(string.Format("{0}-{1}",min,max));。

                    max = min = tmp;。

                }else{。

                    max = tmp;。

                }。

            }

            if ((tmp == min)||(tmp == max)) {。

                arr.Add(string.Format("{0}-{1}",min,max));。

            }

            return string.Join(";",arr.ToArray());。

        }

用法

var str = QueueString("1,2,3,5,7,8,9,15,18,19,24");。

java8 中concurrenthashmap数据结构和HashMap一样,且线程安全 为什么还要HashMap

两种方法:

一行一行读,读到文件尾,你就知道哪行是最好一行了。可以考虑使用System.IO.File.ReadAllLines()方法,返回值是字符串数组。

File.ReadAllLines 方法 (System.IO)。

https://msdn.microsoft.com/zh-cn/library/System.IO.File.ReadAllLines(v=vs.110).aspx。

从流的末尾一个字节一个字节往前读,读到换行符以后,再从这个位置读到文件结尾。

原文地址:http://www.qianchusai.com/queuetoarray-80.html

战纪-120,战纪免费观看完整

战纪-120,战纪免费观看完整

熠熠-70,熠熠生辉是什么意思

熠熠-70,熠熠生辉是什么意思

STM32H743-60

STM32H743-60

hundred-50

hundred-50

thread-580-1-80

thread-580-1-80

PACKING,packing是什么意思中文翻译

PACKING,packing是什么意思中文翻译

pitiable-60

pitiable-60

社鼠文言文-20,社鼠文言文重点字翻译

社鼠文言文-20,社鼠文言文重点字翻译

orphanages-30

orphanages-30

柏直,柏直狗虽老犹能猎,萧溧阳马虽老犹骏

柏直,柏直狗虽老犹能猎,萧溧阳马虽老犹骏

三国志战略版高级建筑攻略 - 升级指南与策略 三国志战略版抵御战法攻略大全 - 游戏战法详解 三国志战略版三战攻略大全 - 最新阵容搭配与武将培养指南 将行其疾 - 三国志战略版战法详解 小米三国志战略版测试服 - 官方测试服信息中心 三国志战略版造币厂最多多少个 - 造币厂数量限制详解 三国志战略版军屯攻略 - 最全军屯建设与资源管理指南 三国志战略版强攻怎么打 - 强攻阵容搭配与战术攻略 三国志战略版测试服下载 - 最新测试资格申请入口 三国志战略版百科大全 - 最全游戏攻略资料库 三国志战略版陈武自愈攻略 - 技能解析与阵容搭配 三国志战略版势力值计算方法详解 - 势力值提升攻略 孙权战法详解 - 三国志战略版攻略指南 三国志战略版孙权战法全攻略 - 专属战法详解与搭配推荐 三国志战略版乐府怎么获得 - 乐府获取方法详解 三国志战略版7级土地势力值计算器 - 势力值攻略指南 三国志战略版势力特性详解 - 魏蜀吴三大势力全攻略 三国志战略版军屯等级详解 - 军屯升级攻略与资源产出表 三国志战略版怎么提升名声 - 完整攻略指南 三国志战略版土地势力值计算器 - 在线计算工具 三国志战略版三军 - 最全攻略、武将搭配、阵容推荐 三国志战略版同盟用法 - 完整攻略指南 三国志战略版分城怎么开 - 完整攻略指南 三国志战略版测试服列表 - 最新测试服务器信息 三国志战略版军屯地产量计算器 - 最全军屯地产量数据与攻略 三国志战略版乐府攻略大全 - 最新阵容搭配与玩法指南 三国志战略版大型军团势力值排行榜 - 最新战力数据分析 三国志战略版军屯怎么增加势力 - 完整攻略指南 三国志战略版怎么快速提高势力值 - 实用攻略指南 三国志战略版拔城秘籍 - 最全攻城攻略指南