以下是检查2个数组的功能。在数组1中搜索一个值,该值的索引用于检索数组2中相同索引处的值
public void raashiNumberRelations(string rashiValue, string[] raashiArray)
{
char[] MobileNum = textBox3.Text.ToCharArray();
Array.Reverse(MobileNum);
int rv = Convert.ToInt32(rashiValue);
string[] ra = raashiArray;
int pos = 0;
string combo1 = "";
string combo2 = "";
string combo3 = "";
if (rv == 1)
{
pos = Array.IndexOf(ra, 5);
combo1 = MobileNum[0].ToString() + MobileNum[pos].ToString();
pos = Array.IndexOf(ra, 8);
combo2 = MobileNum[0].ToString() + MobileNum[pos].ToString();
pos = Array.IndexOf(ra, 11);
combo3 = MobileNum[0].ToString() + MobileNum[pos].ToString();
rashiCombination(combo1, combo2, combo3);
}
if (rv == 2)
{
pos = Array.IndexOf(ra, 4);
combo1 = MobileNum[0].ToString() + MobileNum[pos].ToString();
pos = Array.IndexOf(ra, 7);
combo2 = MobileNum[0].ToString() + MobileNum[pos].ToString();
pos = Array.IndexOf(ra, 10);
combo3 = MobileNum[0].ToString() + MobileNum[pos].ToString();
rashiCombination(combo1, combo2, combo3);
}
if (rv == 3)
{
pos = Array.IndexOf(ra, 3);
combo1 = MobileNum[0].ToString() + MobileNum[pos].ToString();
pos = Array.IndexOf(ra, 9);
combo2 = MobileNum[0].ToString() + MobileNum[pos].ToString();
pos = Array.IndexOf(ra, 12);
combo3 = MobileNum[0].ToString() + MobileNum[pos].ToString();
rashiCombination(combo1, combo2, combo3);
}
if (rv == 4)
{
pos = Array.IndexOf(ra, 8);
combo1 = MobileNum[0].ToString() + MobileNum[pos].ToString();
pos = Array.IndexOf(ra, 11);
combo2 = MobileNum[0].ToString() + MobileNum[pos].ToString();
pos = Array.IndexOf(ra, 2);
combo3 = MobileNum[0].ToString() + MobileNum[pos].ToString();
rashiCombination(combo1, combo2, combo3);
}
if (rv == 5)
{
pos = Array.IndexOf(ra, 7);
combo1 = MobileNum[0].ToString() + MobileNum[pos].ToString();
pos = Array.IndexOf(ra, 10);
combo2 = MobileNum[0].ToString() + MobileNum[pos].ToString();
pos = Array.IndexOf(ra, 1);
combo3 = MobileNum[0].ToString() + MobileNum[pos].ToString();
rashiCombination(combo1, combo2, combo3);
}
if (rv == 6)
{
pos = Array.IndexOf(ra, 9);
combo1 = MobileNum[0].ToString() + MobileNum[pos].ToString();
pos = Array.IndexOf(ra, 12);
combo2 = MobileNum[0].ToString() + MobileNum[pos].ToString();
pos = Array.IndexOf(ra, 3);
combo3 = MobileNum[0].ToString() + MobileNum[pos].ToString();
rashiCombination(combo1, combo2, combo3);
}
if (rv == 7)
{
pos = Array.IndexOf(ra, 11);
combo1 = MobileNum[0].ToString() + MobileNum[pos].ToString();
pos = Array.IndexOf(ra, 2);
combo2 = MobileNum[0].ToString() + MobileNum[pos].ToString();
pos = Array.IndexOf(ra, 5);
combo3 = MobileNum[0].ToString() + MobileNum[pos].ToString();
rashiCombination(combo1, combo2, combo3);
}
if (rv == 8)
{
pos = Array.IndexOf(ra, 10);
combo1 = MobileNum[0].ToString() + MobileNum[pos].ToString();
pos = Array.IndexOf(ra, 1);
combo2 = MobileNum[0].ToString() + MobileNum[pos].ToString();
pos = Array.IndexOf(ra, 4);
combo3 = MobileNum[0].ToString() + MobileNum[pos].ToString();
rashiCombination(combo1, combo2, combo3);
}
if (rv == 9)
{
pos = Array.IndexOf(ra, 12);
combo1 = MobileNum[0].ToString() + MobileNum[pos].ToString();
pos = Array.IndexOf(ra, 3);
combo2 = MobileNum[0].ToString() + MobileNum[pos].ToString();
pos = Array.IndexOf(ra, 6);
combo3 = MobileNum[0].ToString() + MobileNum[pos].ToString();
rashiCombination(combo1, combo2, combo3);
}
if (rv == 10)
{
pos = Array.IndexOf(ra, 2);
combo1 = MobileNum[0].ToString() + MobileNum[pos].ToString();
pos = Array.IndexOf(ra, 5);
combo2 = MobileNum[0].ToString() + MobileNum[pos].ToString();
pos = Array.IndexOf(ra, 8);
combo3 = MobileNum[0].ToString() + MobileNum[pos].ToString();
rashiCombination(combo1, combo2, combo3);
}
if (rv == 11)
{
pos = Array.IndexOf(ra, 1);
combo1 = MobileNum[0].ToString() + MobileNum[pos].ToString();
pos = Array.IndexOf(ra, 4);
combo2 = MobileNum[0].ToString() + MobileNum[pos].ToString();
pos = Array.IndexOf(ra, 7);
combo3 = MobileNum[0].ToString() + MobileNum[pos].ToString();
rashiCombination(combo1, combo2, combo3);
}
if (rv == 12)
{
pos = Array.IndexOf(ra, 3);
combo1 = MobileNum[0].ToString() + MobileNum[pos].ToString();
pos = Array.IndexOf(ra, 6);
combo2 = MobileNum[0].ToString() + MobileNum[pos].ToString();
pos = Array.IndexOf(ra, 9);
combo3 = MobileNum[0].ToString() + MobileNum[pos].ToString();
rashiCombination(combo1, combo2, combo3);
}
}
private void button4_Click(object sender, EventArgs e)
{
string[] mobileNum = new string[12];
string[] raashiRow = new string[12];
addSrNo();
addMobileNumber();
findCombinations();
string zs = calcIndianRaashi(textBox1.Text);
string[] data = new string[12];
string[] CalVal = new string[12];
int temp = 0;
for (int i = ZodiacIndian; i <= 12; i++)
{
data[temp] = i.ToString();
CalVal[temp] = GetCalculatedVal(i).ToString();
temp++;
}
for (int i = 1; i < ZodiacIndian; i++)
{
data[temp] = i.ToString();
CalVal[temp] = GetCalculatedVal(i).ToString();
temp++;
}
Array.Reverse(data);
Array.Reverse(CalVal);
for (int j = 0; j < 12; j++)
{
dataGridView1.Rows[1].Cells[j].Value = data[j];
dataGridView1.Rows.Add();
}
for (int j = 0; j < 12; j++)
{
dataGridView1.Rows[2].Cells[j].Value = CalVal[j];
dataGridView1.Rows.Add();
}
for (int i = 0; i < c.Length; i++)
{
mobileNum[i] = c[i].ToString();
}
raashiRow = data;
raashiNumberRelations(data[11], raashiRow);
displayGrahaEffects(mobileNum);
}
输出给出一个错误数组超出范围,因为pos的值由于某种原因总是保持-1
答案 0 :(得分:0)
如果找不到该项,则函数Array.IndexOf()
可能会返回-1
。您应始终检查此情况并处理错误,可能会打印您要搜索的项目(以及数组中不存在的项目)。
答案 1 :(得分:0)
调用pos = Array.IndexOf(ra,12)时检查raashiArray值的长度;由于您的数组的长度为12,因此它可以具有的最后一个索引是11.放置断点并检查其余的索引。
修改强> 好吧我怎么会错过它。 Array.IndexOf查找字符串的索引,但是您以int格式输入它,因此它无法找到指定的字符串,因此它总是1.在&#34;&#34;中指定数字。还要确保双位数是否存储为单个字符串。 例如
string[] temp = new string[]{ "1", "2", "3", "4", "5" };
int a = Array.IndexOf(temp, "3");
答案 2 :(得分:-1)
Array.IndexOf("My Array",value)
该函数将值视为整数。我的数组是一个字符串数组。我只是将它重新编写为:
Array.IndexOf(MyArray, "value") // Note it now thinks of value as string
中提琴有效。