Arraylist.add()正在覆盖由c#中的循环构建的Arraylist中的所有先前值。代码是:
public ArrayList MakeList(int size, int iterations)
{
ArrayList myList = new ArrayList(iterations);
for (int run = 0; run < iterations; run++)
{
byte[] _byteArray = new byte[size];
bool success = false;
while (!success)
{
//Some Operations
if(condition)
success = true;
else
continue;
if(success)
break;
}
myList .Add(_byteArray );
}
return myList;
}
上面的循环总是用最新的字节数组覆盖List中的值。请帮我解决这个问题。
答案 0 :(得分:0)
public class Program
{
public static void Main(string[] args)
{
var col = new ColTest();
col.MakeList();
}
}
public class ColTest
{
public ArrayList MakeList()
{
var capacity = 3;
ArrayList myList = new ArrayList(capacity);
int size = 2;
for (int run = 0; run < capacity; run++)
{
byte[] _byteArray = new byte[size];
if (run == 0)
{
_byteArray[0] = 0;
_byteArray[1] = 0;
}
else if (run == 1)
{
_byteArray[0] = 0;
_byteArray[1] = 1;
}
else if (run == 2)
{
_byteArray[0] = 1;
_byteArray[1] = 1;
}
myList.Add(_byteArray);
}
return myList;
}
}
希望这有帮助!