- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
给定这个简单的同步代码:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Mutex
{
class CalcArrayThreads
{
private static System.Threading.Mutex mutex = new System.Threading.Mutex();
private const int ThreadsNumber = 10;
private static List<int> list = new List<int>(10);
public static void SumPartialArray(object index)
{
mutex.WaitOne();
int indexBoxed = (int) index;
int sum = 0;
for (int i = indexBoxed; i < indexBoxed + 9; i++)
{
sum += i;
}
Console.WriteLine(string.Format("Thread : {0} Calculated value : {1}", Thread.CurrentThread.Name, sum));
// list.Add(sum);
list[indexBoxed] = sum;
mutex.ReleaseMutex();
// Console.WriteLine(list.Count());
}
static void Main(string[] args)
{
for (int i = 0; i < ThreadsNumber; i++)
{
Thread myThread = new Thread(new ParameterizedThreadStart(SumPartialArray));
myThread.Name = String.Format("Thread{0}", i + 1);
myThread.Start(i);
}
Console.Read();
}
}
}
当我使用这条线时:
list[indexBoxed] = sum;
我得到:
Unhandled Exception: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
即使列表的容量是 10 。
为什么?
最佳答案
如果您只是对数组执行以下操作,它会按预期工作,设置第二个元素:
int[] list = new int[10];
list[2] = 5; // second element is 5, no exception thrown
查看 List<T>
构造函数,当你传入一个容量时,它实际上是在使用一个内部数组做一些非常相似的事情:
this._items = new T[capacity];
因此,当您尝试访问小于容量的任何元素时,它似乎应该工作,但索引器的实现方式如下:
public T this[int index]
{
get
{
if (index >= this._size)
throw new ArgumentOutOfRangeException("...");
return this._items[index];
}
set
{
if (index >= this._size)
throw new ArgumentOutOfRangeException("...");
this._items[index] = value;
}
}
它实际上是在检查 _size
首先是变量,如果 index
则抛出异常你要求的比它大。如果没有那张支票,它会按您预期的那样工作。
_size
除非将非空集合传递给构造函数,否则初始化为 0,并在使用 Add
时更改值, RemoveAt
, Clear
等方法。在内部,它仍然只是使用数组来存储元素,如果 _size
大于容量(例如,在尝试 Add
一个元素之后),它会分配一个新数组并将旧(较小)数组中的所有元素复制到其中。
我看到您可以考虑使用两种解决方案:
要么只使用一个数组,像这样:
private static int[] list = new int[10];
或者通过构造函数提供一些默认值的集合(这里是一堆零):
private static List<int> list = new List<int>(Enumerable.Repeat(0, 10));
关于c# - 即使使用具有容量的构造函数,列表 C# 容量始终为 0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33051064/
当我使用路径文件上的快捷方式在文件之间移动时,似乎我不仅仅是在文件之间移动。 我使用>转到一个文件,在该文件中我更改光标的位置并执行某些操作,然后按 gf noremap 关于vim 通过快捷方式直
我正在尝试使用 Pong P. Chu 的书来学习 Verilog。我有一个关于如何评估和实现始终 block 的问题。作者代码中的风格让我感到困惑。 在此示例中,他编写了一个具有两个输出寄存器“y1
我正在尝试制作一个聊天应用程序,因此我需要它始终接收服务器信息。因此,当请求完成时,在: http.onreadystatechange=function(){ 我再次调用该函数,因此: reques
当您在 always block 敏感度列表中使用通配符 @* 时,我对什么被视为输入有点困惑。例如,在下面的示例中,哪些信号被解释为导致 always block 被重新评估的输入? 据我了解,cl
我有一个充当调试器的程序。我为线程设置了一个 hw bp,将 dr0 设置为我希望 bp 所在的地址,将 dr7 设置为 1,因为我希望 bp 在每次执行该地址时生成一个事件。 它有效,但现在的问题是
如何每次都以管理员身份在 Windows 上运行 git bash。 操作系统 - Windows 10 家庭版 64 位 最佳答案 我在 Google 上找到了这个结果: 将 Git Bash 设置
使用 accept() 时或 getpeername() , sockaddr_storage总是有 ss_family=AF_INET6 : struct sockaddr_storage addr
我在 Cordova 方面还有另一个问题。我想在 Cordova 7.1.0 中使用插件“cordova.custom.plugins.exitapp”和“cordova-plugins-printe
我试图让模块通过 ISE 12.4 中的语法检查,但它给了我一个我不明白的错误。首先是代码片段: parameter ROWBITS = 4; reg [ROWBITS-1:0] temp; genv
我正在使用Cordova开发适用于iOS的应用程序,其中包括地理位置功能(我使用官方插件https://github.com/apache/cordova-plugin-geolocation)。我在
我想知道是否有可能只在敏感列表中的多个信号一起变化时才执行 always block 。 例如,假设我有一个信号“in”和另一个“posedge clk”。我希望在两个信号都发生变化时执行 alway
我需要实现一种算法来访问数据库来检查最后一个元素,以便计算新的元素。当然,第一次这是不可能的,因为数据库是空的,我得到 IndexOutOfBoundsException) index 0 reque
我正在利用我在网上找到的画廊系统,根据鼠标图像的接近程度,它会按比例增长。 链接:Gallery 好吧,我调整了代码以响应(如您所见正在 build 中)并且没有明显的问题。我的问题在更改分辨率时开始
我正在创建一个 kiosk 应用程序,我想确保它无论如何始终位于其他 Windows 应用程序和 Windows 任务栏之上。 我已经阻止了 Windows 键盘命令(alt-tab 等),但仍有可能
我即将开始一个新的 React 项目,并尝试利用我以前的知识来创建一些关于我如何构建应用程序的规则。 有些事情我认为是真的: Redux 保存整个应用程序的“主要”数据 如果需要跨应用程序共享,Red
当你打开 VS Code 时,终端默认是在底部打开的。您可以单击该图标将其向右移动。我想知道是否有办法将右侧打开设置为默认值。 谢谢。 最佳答案 是的 - 在 v1.20 中引入了设置 workb
我有一个Events表,其中包含各种类型的事件。我只关心其中一种类型。因此,我编写的每个查询都以开头 Events.objects.filter(event_type="the_type").\
我在单例中创建了一个Timer,并且我一直在努力解决为什么Timer没有触发。我查看了这里的帖子,但没有找到我认为可以直接回答我的问题的帖子。 class ConnectionStateMonitor
我在 TableViewController 中显示了一组项目。它们在 TVC 中正确显示。下面的代码会继续,但它只会继续到我的 MKMapItem 数组的 indexPath 0,而不是被单击的单元
我的 VC 是这样的: var coins = 50 // coins override func viewDidLoad() { super.viewDidLoad() if(SKP
我是一名优秀的程序员,十分优秀!