扩展枚举范围

发布于 2024-12-24 20:15:38 字数 569 浏览 4 评论 0原文

可能的重复:
Enumerable.Range 实现

我遇到了一个问题,我需要添加一个很长的值(Int64) 到一个列表中,值为 600851475143,我想创建一个包含的 List所有 int 都达到这个值,但是 Enumerable.Range 有一个限制,在 count 参数中,它只接受 int 值,因为我距离该值很远,我决定循环遍历列表和所有这些值,但我的系统很快就会耗尽内存,我该怎么办?

List<int64> lst = new List<int64>();

for (Int64 i = 3; i < 600851475143; i=i+2)
{
    lst.Add(i);
}

谢谢

Possible Duplicate:
Enumerable.Range implementation

I ran into an issue where i need to add a very long value (Int64) into a list, The value is 600851475143, i want to create a List<Int64> that contains all the int upto this value, but Enumerable.Range has a limitation that in the count parameter, it accept only int values, as i am far from that value, i decided to loop through the list and and all those values, but my system soons run out of memory, what should i do?

List<int64> lst = new List<int64>();

for (Int64 i = 3; i < 600851475143; i=i+2)
{
    lst.Add(i);
}

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回忆那么伤 2024-12-31 20:15:38

正如评论中指出的,如此大的数组将需要大量的内存。例如,更好的选择是为自己创建一个枚举器来循环范围。开始查看此页面,了解如何: http://msdn.microsoft.com/ en-us/library/9k7k7cf0.aspx :-)

As pointed out in the comments, such a large array would require an enormous amount of memory. A better option would for instance be to create yourself an enumerator to loop over the range. Start looking at this page for how to: http://msdn.microsoft.com/en-us/library/9k7k7cf0.aspx :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文