【问题标题】:C# Error CS1503: Argument 1: cannot convert from int[] to intC# 错误 CS1503:参数 1:无法从 int[] 转换为 int
【发布时间】:2017-12-20 06:43:36
【问题描述】:

当我尝试显示数组中的项目时,它会显示:错误 CS1503:参数 1:无法从 int[] 转换为 int

    List<int[]> rolls = new List<int[]>();
    rolls.Add(new int[] { throw1, throw2, throw3 }); 


    Console.WriteLine(rolls[rolls[1]]);

【问题讨论】:

  • 这不是rolls[throw2]吗?
  • 你没有卷[1]。你有一个包含一个元素(即rolls [0])的列表rolls,该元素是{throw1,throw2,throw3}。
  • @andrewf 在更改时仍然会出现同样的错误。

标签: c#


【解决方案1】:

您正在尝试使用对数组的引用来索引列表。 当你写 rolls[rolls[1]] 时,rolls[1] 是一个 array,而不是一个 int。当您索引到列表中时(即当您滚动 [x] 时),其中 x 是某个 int 时,预计会使用 int。此外,正如其他人指出的那样,由于 rolls 只包含一个元素,rolls[1] 将是一个超出范围的索引。 什么会起作用:

Console.WriteLine(rolls[0][0]) ; 

【讨论】:

    【解决方案2】:

    基于编译错误,我假设您正试图从数组(位于列表内)中拉出一个元素。

    例如,

    List<int[]> rolls = new List<int[]>();
    rolls.Add(new int[] { 1, 2, 3 });
    

    要从上述数组中获取一个元素(根据您的代码),请使用

    rolls[0][0]
    

    第一个'[0]'将从List中获取'0th'索引处的元素(在本例中为int[]),下一个索引'[0]'将从int[]中获取'0th'元素.

    【讨论】:

      猜你喜欢
      • 2014-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-12
      • 2018-11-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多