c# 列表索引 0 显示值为 -1
c# list index 0 showing value as -1
我正在统一创建一个基本程序,我在其中检查列表数组索引根据值执行特定操作,然后我增加 index.The 我遇到的问题是数组总是存储在索引 0 且始终等于 -1。
public class numGen(){
int val;
int i = 0;
System.Random rnd = new System.Random();
val = rnd.Next(1, 5);
arrayList.Add(val);
Debug.Log("val"+val);
Debug.Log("array"+arrayList.IndexOf(i));
if (arrayList.IndexOf(i) == 1)
{
Debug.Log("action1");
i++;
}
else if (arrayList.IndexOf(i) == 2)
{
Debug.Log("action2");
i++;
}
//and so on
}
我在日志中使用了debug,所以val会输出一个预期的值,例如2,当我检查存储在索引中的值是 -1 时,它被添加到数组中。
不确定 int 值如何变化或为何变化。
arrayList.IndexOf(i)
给出 value 为 i
的任何元素的索引。如果您想要 index i
处的值,则必须改用 arrayList[I]
。
这里的问题是误解了 ArrayList
和 IndexOf()
的工作原理。
ArrayList.IndexOf()
Searches for the specified Object and returns the zero-based index of the first occurrence within the range of elements in the ArrayList that starts at the specified index and contains the specified number of elements.
所以你的异常来自:
The zero-based index of the first occurrence of value within the range of elements in the ArrayList that starts at startIndex and contains count number of elements, if found; otherwise, -1.
我认为您正在寻找这样的东西:
ArrayList.Item()
Gets or sets the element at the specified index.
也可以
This property provides the ability to access a specific element in the collection by using the following syntax: myCollection[index].
您的例子:
if (arrayList[i] == 1)
{
// if true..
}
IndexOf 给出你给的值的索引。如果在数组中没有找到相等的值它 returns -1
ArrayList arr = new ArrayList();
arr.Add(5);
arr.Add(2);
var isMinusOne = arr.IndexOf(0); //Is -1
var isZero = arr.IndexOf(5); //Is 0
我正在统一创建一个基本程序,我在其中检查列表数组索引根据值执行特定操作,然后我增加 index.The 我遇到的问题是数组总是存储在索引 0 且始终等于 -1。
public class numGen(){
int val;
int i = 0;
System.Random rnd = new System.Random();
val = rnd.Next(1, 5);
arrayList.Add(val);
Debug.Log("val"+val);
Debug.Log("array"+arrayList.IndexOf(i));
if (arrayList.IndexOf(i) == 1)
{
Debug.Log("action1");
i++;
}
else if (arrayList.IndexOf(i) == 2)
{
Debug.Log("action2");
i++;
}
//and so on
}
我在日志中使用了debug,所以val会输出一个预期的值,例如2,当我检查存储在索引中的值是 -1 时,它被添加到数组中。
不确定 int 值如何变化或为何变化。
arrayList.IndexOf(i)
给出 value 为 i
的任何元素的索引。如果您想要 index i
处的值,则必须改用 arrayList[I]
。
这里的问题是误解了 ArrayList
和 IndexOf()
的工作原理。
ArrayList.IndexOf()
Searches for the specified Object and returns the zero-based index of the first occurrence within the range of elements in the ArrayList that starts at the specified index and contains the specified number of elements.
所以你的异常来自:
The zero-based index of the first occurrence of value within the range of elements in the ArrayList that starts at startIndex and contains count number of elements, if found; otherwise, -1.
我认为您正在寻找这样的东西:
ArrayList.Item()
Gets or sets the element at the specified index.
也可以
This property provides the ability to access a specific element in the collection by using the following syntax: myCollection[index].
您的例子:
if (arrayList[i] == 1)
{
// if true..
}
IndexOf 给出你给的值的索引。如果在数组中没有找到相等的值它 returns -1
ArrayList arr = new ArrayList();
arr.Add(5);
arr.Add(2);
var isMinusOne = arr.IndexOf(0); //Is -1
var isZero = arr.IndexOf(5); //Is 0