如何在同一次迭代中保存数组中的2个不同数据 [英] How to save 2 different datas in an array in the same iteration

查看:71
本文介绍了如何在同一次迭代中保存数组中的2个不同数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



有人能告诉我如何在一次迭代中在数组中保存2个不同的数据吗?

喜欢例如:我有一个阵列给我fieldindex&在满足任何if条件时设置的计时器。问题是指令何时进入for循环&如果条件通过则大于1,则fieldindex中的值将被覆盖,从而丢失了我之前的数据。以下代码可能会给出清晰的图片。

Hi all,

Can anybody tell me how to save 2 different datas in an array in a single iteration?
Like for eg: I have an array which gives me fieldindex & a timer which is set when any of the if conditions are satisfied. The problem is when the instruction enters the for loop & if more than 1 if conditions passes then the value in fieldindex will be over written, loosing my previous data. The below code may give a clear picture.

   for(int i=0; i<24; i++)
    {
 if (SID >= 10 && SID <= 11)
       {
           fieldindex1[i] = 1;
           timer1.Start();
       }


if (SID >= 12 && SID <= 13)
       {
           fieldindex1[i] = 2;
           timer1.Start();
       }

if (SID >= 14 && SID <= 15)
       {
           fieldindex1[i] = 3;
           timer1.Start();
       }
   }



所以他们的名字是一个数组fieldindex1 []&扫描时,如果条件或任何1条if条件或所有if条件,它可能会输入1st。上面的方法不会获取所需的数据。我希望它以其他方式完成。如果它进入1st if条件,则应将fieldindex [i]值加载到它进一步增加以适应新值。另外我不想为不同的样式使用不同的数组。


So their is an array by name fieldindex1[] & while scanning it might enter 1st if condition or any 1 of the if condition or all the if conditions. The above approach will not fetch me the required data.. I want it be done in some other way.. If it enters the 1st if condition then the fieldindex[i] value should be loaded to it n further incremented to accommodate new value. Also i dont want to use different arrays for different styles.

推荐答案

你好!!!



我无法理解你的要求......但你可以使用List保存你的数据



Hello!!!

I couldn''t understand your requirement clearly... however you can preserve your data using List

List<int> fieldindex1 = new List<int>();
            for (int i = 0; i < 24; i++)
            {
                if (SID >= 10 && SID <= 11)
                {
                    fieldindex1.Add(1);
                    //fieldindex1[i] = 1;
                    timer1.Start();
                }


                if (SID >= 12 && SID <= 13)
                {
                    fieldindex1.Add(2);
                    //fieldindex1[i] = 2;
                    timer1.Start();
                }

                if (SI >= 14 && SID <= 15)
                {
                    fieldindex1.Add(3);
                    //fieldindex1[i] = 3;
                    timer1.Start();
                }
            }


int count = 0;

for(int i = 0; i< 24; i ++)

{

if(SID> = 10&& SID< = 11)

{

fieldindex1 [count ++] = 1;

timer1.Start();

}





if(SID> = 12&& SID< = 13)

{

fieldindex1 [count ++] = 2;

timer1.Start();

}



if(SI> = 14&& SID< = 15)

{

fieldindex1 [count ++] = 3;

timer1.Start();

}

}
int count=0;
for(int i=0; i<24; i++)
{
if (SID >= 10 && SID <= 11)
{
fieldindex1[count++] = 1;
timer1.Start();
}


if (SID >= 12 && SID <= 13)
{
fieldindex1[count++] = 2;
timer1.Start();
}

if (SI >= 14 && SID <= 15)
{
fieldindex1[count++] = 3;
timer1.Start();
}
}


这篇关于如何在同一次迭代中保存数组中的2个不同数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆