C# 插入列表中的索引位置

C# 插入列表中的索引位置,c#,list,insert,C#,List,Insert,我正在尝试使用Insert覆盖列表中的值,但结果不符合预期 运行代码后,列表中有6个值,但应该只有4个 测试: 阶级 公共类模块结果 { 公共字符串modName{get;set;} 公共整数积分{get;set;} 公共字符串名称{get;set;} 列表分数=新列表(); 公共重写字符串ToString() { 字符串输出=“模块名称:”+modName+”,学分:“+Credits+”,名称:“+Name+”\nCA结果:\n”; for(int i=0;i

我正在尝试使用Insert覆盖
列表中的值,但结果不符合预期

运行代码后,
列表中有6个值,但应该只有4个

测试:

阶级

公共类模块结果
{
公共字符串modName{get;set;}
公共整数积分{get;set;}
公共字符串名称{get;set;}
列表分数=新列表();
公共重写字符串ToString()
{
字符串输出=“模块名称:”+modName+”,学分:“+Credits+”,名称:“+Name+”\nCA结果:\n”;
for(int i=0;i分数.计数)
{
抛出新异常(“获取无效CA号”);
}
其他的
返回分数[i];
}
设置
{
int指数=i-1;
如果(指数<0 | |指数>分数.计数)
{
抛出新异常(“无效CA编号集”);
}
其他的
{ 
分数。插入(索引、值);
}
}
}
}

我现在尝试了几种不同的方法,但无法解决问题。

Insert
不会覆盖,它会在索引处插入并“推”索引中的所有内容。因此,当您在索引
2
处插入时,它会将项目添加到列表中,并将索引
2
处的内容推送到索引
3
,将
3
处的内容推送到
4
,依此类推

相反,您可以结合使用
添加
和索引到列表中来完成您想要的:

if(scores.Count < index)
{
    //If the index you're accessing is past the end of the list, use Add
    scores.Add(value);
}
else
{
    //Otherwise let's overwrite what's already at index i
    scores[index] = value;
}
if(scores.Count
插入
不会覆盖,它会在索引处插入并“推”索引中的所有内容。因此,当您在索引
2
处插入时,它会将项目添加到列表中,并将索引
2
处的内容推送到索引
3
,将
3
处的内容推送到
4
,依此类推

相反,您可以结合使用
添加
和索引到列表中来完成您想要的:

if(scores.Count < index)
{
    //If the index you're accessing is past the end of the list, use Add
    scores.Add(value);
}
else
{
    //Otherwise let's overwrite what's already at index i
    scores[index] = value;
}
if(scores.Count
List.Insert方法在指定位置插入新项。它不会在该位置替换原始元件

所以这个代码:

scores.Insert(index, value);
只需在索引位置插入新值,将列表中的所有其他值进一步移动一个位置

要仅使用4个元素替换为按索引分配的元素,请执行以下操作:

scores[index] = value;

插入方法在指定位置插入新项。它不会在该位置替换原始元件

所以这个代码:

scores.Insert(index, value);
只需在索引位置插入新值,将列表中的所有其他值进一步移动一个位置

要仅使用4个元素替换为按索引分配的元素,请执行以下操作:

scores[index] = value;

我认为你的索引应该是这样的

public double this[int i]
{  
    get
    {
        int index = i - 1;
        if (index < 0 || index >= scores.Count)
        {
            throw new Exception("Invalid CA number get");
        }
        else
            return scores[i]; 
    }
    set
    {
        int index = i - 1;
        if (index < 0 || index > scores.Count)
        {
            throw new Exception("Invalid CA number set");
        }
        else
        { 
            if (index < scores.Count)
                scores[index] = value;
            else
                scores.Add(value);
        }
    }
}
public双倍于此[int i]
{  
得到
{
int指数=i-1;
如果(索引<0 | |索引>=分数.计数)
{
抛出新异常(“获取无效CA号”);
}
其他的
返回分数[i];
}
设置
{
int指数=i-1;
如果(指数<0 | |指数>分数.计数)
{
抛出新异常(“无效CA编号集”);
}
其他的
{ 
如果(指数<分数.计数)
分数[指数]=数值;
其他的
分数。增加(价值);
}
}
}

我认为您的索引器应该是这样的

public double this[int i]
{  
    get
    {
        int index = i - 1;
        if (index < 0 || index >= scores.Count)
        {
            throw new Exception("Invalid CA number get");
        }
        else
            return scores[i]; 
    }
    set
    {
        int index = i - 1;
        if (index < 0 || index > scores.Count)
        {
            throw new Exception("Invalid CA number set");
        }
        else
        { 
            if (index < scores.Count)
                scores[index] = value;
            else
                scores.Add(value);
        }
    }
}
public双倍于此[int i]
{  
得到
{
int指数=i-1;
如果(索引<0 | |索引>=分数.计数)
{
抛出新异常(“获取无效CA号”);
}
其他的
返回分数[i];
}
设置
{
int指数=i-1;
如果(指数<0 | |指数>分数.计数)
{
抛出新异常(“无效CA编号集”);
}
其他的
{ 
如果(指数<分数.计数)
分数[指数]=数值;
其他的
分数。增加(价值);
}
}
}

当索引等于列表中的项目数时,List类上的Insert方法将在末尾插入项目。
有关出错原因的详细信息,请参见

当索引等于列表中的项目数时,List类上的Insert方法将在末尾插入项目。
有关出错原因的更多信息,请参见

你在做什么在我看来更像是一本
字典
,而不是列表行为
插入
将在索引处添加值,不覆盖索引处的值。在我看来,你在做什么更像是一个
字典
,而不是列表行为
插入
将在索引处添加值,不覆盖索引处的值。当您尝试添加第一项时,此操作将失败,并出现
ArgumentOutOfRangeException
。对于问题中的示例,此操作不会失败。我没有对算法提出任何建议,只是指定了一个工具。当您尝试添加第一项时,这将以
ArgumentOutOfRangeException
失败。对于问题中的示例,这不会失败。我没有对算法提出任何建议,只是指定了一个工具。我有一些类似于您的代码的东西,我得到的索引超出了范围错误,我只是插入了您的代码a