如何在变量中均匀分布元素数组 [英] How do I distribute array of elements evenly across a variable

查看:85
本文介绍了如何在变量中均匀分布元素数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下列表

 List< int> lstuser =  new  List< int>(); 
lstuser.Add( 101 );
lstuser.Add( 102 );
lstuser.Add( 103 );
lstuser.Add( 104 );



int assigntovariable = 0;



我需要随后将上面的列表值分配给变量。这是变量将在for循环和&它的长度可以从1到任何不等。



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

{

assigntovariable = // lstuser从一个接一个按顺序

assignedtouser = 101,如果i = 0

assignedtouser = 102,如果i = 1

assignedtouser = 103,如果i = 2

assignedtouser = 104,如果i = 3

assignedtouser = 101,如果i = 4

assignedtouser = 102,如果i = 5



..将继续直到循环结束



}



以下是我试过的...但是我觉得它不是很好..请给我一个更好的解决方案..谢谢你提前..



< pre lang =   C#>列表与安培; LT; INT&安培; GT; lstuser =  new  List& lt; int& gt;(); 
lstuser.Add( 101 );
lstuser.Add( 102 );
lstuser.Add( 103 );
lstuser.Add( 104 );
int userselected = 0 ;
int j = 0 ,x = 0 ;

for int i = 0 ; i& lt; 10 ; i ++)
{
if (x == 0 || x == lstuser.Count)
x = 0 ;
for (j = x; j& lt; lstuser.Count; j ++)
{
if (i& gt; j)
{
userselected = lstuser [j];
if (x == lstuser.Count)
x = 0 ;
x = j + 1 ;
}
else
{
userselected = lstuser [i];
x = j + 1;
if (x == lstuser.Count)
x = 0 ;
}
break ;
}

int s = userselected;
} < / pre >





我的尝试:



 List< int> lstuser =  new  List< int>(); 
lstuser.Add( 101 );
lstuser.Add( 102 );
lstuser.Add( 103 );
lstuser.Add( 104 );
int userselected = 0 ;
int j = 0 ,x = 0 ;

for int i = 0 ; i < 10 ; i ++)
{
if (x == 0 || x == lstuser.Count)
x = < span class =code-digit> 0
;
for (j = x; j < lstuser.Count; j ++)
{
if (i > j)
{
userselected = lstuser [j];
if (x == lstuser.Count)
x = 0 ;
x = j + 1 ;
}
else
{
userselected = lstuser [i];
x = j + 1;
if (x == lstuser.Count)
x = 0 ;
}
break ;
}

int s = userselected;
}

解决方案

最简单的方法是使用模数运算符'%'

< pre lang =C#> List< int> lstuser = new List< int>();
lstuser.Add( 101 );
lstuser.Add( 102 );
lstuser.Add( 103 );
lstuser.Add( 104 );
for int i = 0 ; i < 10 ; i ++)
{
int assignedToUser = lstUser [i%lstUser.Count];
...
}



模数运算符在除法后返回余数:因此x%10将始终为0到9,x% 3将是0到2,x%lstUser.Count将为0到(列表中的数字元素减1),这是列表中索引所需的内容。


I have below list

List<int> lstuser = new List<int>();
lstuser.Add(101);
lstuser.Add(102);
lstuser.Add(103);
lstuser.Add(104);


int assigntovariable =0;

I need to assign the above list values to a variable subsequently. That is variable will be inside a for loop & and its length may vary from 1 to any.

for (int i =0;i<10;i++)
{
assigntovariable = //lstuser from one by one in order as
assignedtouser = 101, if i = 0
assignedtouser = 102, if i = 1
assignedtouser = 103, if i = 2
assignedtouser = 104, if i = 3
assignedtouser = 101, if i = 4
assignedtouser = 102, if i = 5

..it will continue till the end of loop

}

Below is what i tried ..but its not good one i think.. please provide me a better solution..Thanks in advance..

<pre lang="C#">List&lt;int&gt; lstuser = new List&lt;int&gt;();
            lstuser.Add(101);
            lstuser.Add(102);
            lstuser.Add(103);
            lstuser.Add(104);
            int userselected = 0;
            int j = 0, x = 0;

            for (int i = 0; i &lt; 10; i++)
            {
                if (x == 0 || x == lstuser.Count)
                     x = 0;
                for (j = x; j &lt; lstuser.Count; j++)
                {
                    if (i &gt; j)
                    {
                        userselected = lstuser[j];
                        if (x == lstuser.Count)
                            x = 0;
                        x = j + 1;
                    }
                    else
                    {
                        userselected = lstuser[i];
                        x = j+1;
                        if (x == lstuser.Count)
                            x = 0;
                    }
                    break;
                }

                int s = userselected;
            }</pre>



What I have tried:

List<int> lstuser = new List<int>();
            lstuser.Add(101);
            lstuser.Add(102);
            lstuser.Add(103);
            lstuser.Add(104);
            int userselected = 0;
            int j = 0, x = 0;

            for (int i = 0; i < 10; i++)
            {
                if (x == 0 || x == lstuser.Count)
                     x = 0;
                for (j = x; j < lstuser.Count; j++)
                {
                    if (i > j)
                    {
                        userselected = lstuser[j];
                        if (x == lstuser.Count)
                            x = 0;
                         x = j + 1;
                    }
                    else
                    {
                        userselected = lstuser[i];
                        x = j+1;
                        if (x == lstuser.Count)
                            x = 0;
                    }
                    break;
                }

                int s = userselected;
            }

解决方案

The easiest way is to use the modulus operator '%'

List<int> lstuser = new List<int>();
lstuser.Add(101);
lstuser.Add(102);
lstuser.Add(103);
lstuser.Add(104);
for (int i = 0; i < 10; i++)
   {
   int assignedToUser = lstUser[i % lstUser.Count];
   ...
   }


The Modulus operator returns the remainder after a division: so x % 10 will always be 0 to 9, x % 3 will be 0 to 2, and x % lstUser.Count will be 0 to (number elements in the List minus one), which is what you need for an index into the list.


这篇关于如何在变量中均匀分布元素数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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