C#:ArrayList.Clear()清除错误的数组
我正在用C#中的ArrayList做一些事情。 我有两个ArrayList(align和best),在特定时间内,我在“for”例程中生成best=align 问题是,在循环结束时,我确实对齐了。清除,但在这一次,数组“最佳”也被清除。循环之后,当我不得不使用数组“best”时,我遇到了麻烦,因为它被清除了,我试图访问它的索引 有什么问题吗 下面是我的一段代码:C#:ArrayList.Clear()清除错误的数组,c#,arrays,arraylist,C#,Arrays,Arraylist,我正在用C#中的ArrayList做一些事情。 我有两个ArrayList(align和best),在特定时间内,我在“for”例程中生成best=align 问题是,在循环结束时,我确实对齐了。清除,但在这一次,数组“最佳”也被清除。循环之后,当我不得不使用数组“best”时,我遇到了麻烦,因为它被清除了,我试图访问它的索引 有什么问题吗 下面是我的一段代码: public string AntColony() { ArrayList align = new ArrayList();
public string AntColony()
{
ArrayList align = new ArrayList();
ArrayList best = new ArrayList();
for(int z=0;z<n_ants;z++)
{
//do the things i have to do
//full the array "align" with something (this will have two "adds", so, this array is a 2 lines array)
score = Score(align);
UpdatePhero(tao, path, score);
if (score > score_before)
{
score_before = score;
best = align;
}
align.Clear(); //clear the array align
}
string s = best[0].ToString() + "\r\n\r\n" + best[1].ToString() + "\r\n\r\n Number of matches: " + n_matches + "\r\n\r\n Score: " + score;
return s;
}
公共字符串()
{
ArrayList align=新的ArrayList();
ArrayList best=新的ArrayList();
对于(整数z=0;之前的z分数)
{
得分前=得分;
最佳=对齐;
}
align.Clear();//清除数组align
}
字符串s=最佳[0]。ToString()+“\r\n\r\n”+最佳[1]。ToString()+”\r\n\r\n匹配数:“+n\u匹配+”\r\n\r\n分数:“+Score;
返回s;
}
谢谢大家! 数组变量是引用类型。调用
best=align
时,您并不是将align
的内容复制到array
,而是使它们指向相同的位置,即它们引用相同的内存位置
请尝试使用数组变量作为引用类型。调用
best=align
时,您并不是将align
的内容复制到array
,而是使它们指向相同的位置,即它们引用相同的内存位置
请尝试
best=align.Clone()
因为align是临时的,所以可以在调用Score
之前重新创建它,并在需要时指定给best:
public string AntColony()
{
ArrayList best = null;
for(int z=0;z<n_ants;z++)
{
//do the things i have to do
//full the array "align" with something (this will have two "adds", so, this array is a 2 lines array)
ArrayList align = new ArrayList();
score = Score(align);
UpdatePhero(tao, path, score);
if (score > score_before)
{
score_before = score;
best = align;
}
}
if (best != null)
{
string s = best[0].ToString() + "\r\n\r\n" + best[1].ToString() + "\r\n\r\n Number of matches: " + n_matches + "\r\n\r\n Score: " + score;
return s;
}
// TODO: Report failure here
return null;
}
公共字符串()
{
ArrayList best=null;
对于(整数z=0;之前的z分数)
{
得分前=得分;
最佳=对齐;
}
}
如果(最佳!=null)
{
字符串s=最佳[0]。ToString()+“\r\n\r\n”+最佳[1]。ToString()+”\r\n\r\n匹配数:“+n\u匹配+”\r\n\r\n分数:“+Score;
返回s;
}
//TODO:在此处报告失败
返回null;
}
因为align是临时的,所以可以在调用Score
之前重新创建它,并在需要时分配给best:
public string AntColony()
{
ArrayList best = null;
for(int z=0;z<n_ants;z++)
{
//do the things i have to do
//full the array "align" with something (this will have two "adds", so, this array is a 2 lines array)
ArrayList align = new ArrayList();
score = Score(align);
UpdatePhero(tao, path, score);
if (score > score_before)
{
score_before = score;
best = align;
}
}
if (best != null)
{
string s = best[0].ToString() + "\r\n\r\n" + best[1].ToString() + "\r\n\r\n Number of matches: " + n_matches + "\r\n\r\n Score: " + score;
return s;
}
// TODO: Report failure here
return null;
}
公共字符串()
{
ArrayList best=null;
对于(整数z=0;之前的z分数)
{
得分前=得分;
最佳=对齐;
}
}
如果(最佳!=null)
{
字符串s=最佳[0]。ToString()+“\r\n\r\n”+最佳[1]。ToString()+”\r\n\r\n匹配数:“+n\u匹配+”\r\n\r\n分数:“+Score;
返回s;
}
//TODO:在此处报告失败
返回null;
}
您正在使最佳
参考指向对齐
。C#基于参考文献!谢谢你的回答。我能做些什么来解决这个问题?我是说,什么策略?无论如何,我必须使best=align并在最后清除align数组。您的查询已经由一位作者在答案部分解决。祝你好运。同时检查一下你正在做的最佳
参考,指向对齐
。C#基于参考文献!谢谢你的回答。我能做些什么来解决这个问题?我是说,什么策略?无论如何,我必须使best=align并在最后清除align数组。您的查询已经由一位作者在答案部分解决。祝你好运。同时也谢谢你的回答!我尝试了best=align.Clone(),但我得到的结果是,无法将“object”转换为“convert.Collections.ArrayList”。它工作得最好=(ArrayList)align.Clone(),非常感谢您,我能帮上忙!谢谢你的回答!我尝试了best=align.Clone(),但我得到的结果是,无法将“object”转换为“convert.Collections.ArrayList”。它工作得最好=(ArrayList)align.Clone(),非常感谢您,我能帮上忙!谢谢你的回答!不幸的是,我需要数组在循环之前对齐来做其他事情!谢谢你的回答!不幸的是,我需要数组在循环之前对齐来做其他事情!