for循环遍历数组 [英] Array traversing by for loop

查看:110
本文介绍了for循环遍历数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组,假设"ar"具有许多元素.我想根据整数变量遍历或处理数组元素,假设"a"可以具有0或1或2或3或4或5或6,依此类推.....

假设ar具有元素1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23, 24,25,26,27,28,29,30,31 ...

当a = 0时我想要什么然后循环处理ar
的元素1 如果a = 1 thn处理ar的元素2,3
如果a = 2 thn处理ar
的元素4,5,6,7 如果a = 3 thn处理ar的元素8,9,10,11,12,13,14,15 如果a = 4 thn处理ar的元素16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31 等等...

请有人来做,谢谢他

在此先感谢.........

请每个人的代码项目都是一个我急切需要解决的最有思想的社区,请帮助..........

I have an array suppose "ar" which has a number of elements.I want to travers or process the array elements according to a integer variable suppose "a" which can have 0 or 1 or 2 or 3 or 4 or or 5 or 6 and so on.....

Suppose ar has elements 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31...

What i want when a=0 then the loop process the element 1 of ar
If a=1 thn process the elements 2,3 of ar
If a=2 thn process the elements 4,5,6,7 of ar
if a=3 thn process the elements 8,9,10,11,12,13,14,15 of ar
If a=4 thn process the elements 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31 of ar
and so on......

Please if any one can do this i m thankful to him

thanks a lot in advance.........

Please everyone code project is a community with the greatest minds i need to solve this problem urgently please help..........

推荐答案

在我看来就像在接下来的简单中一样,您需要将count变量乘以2.
您必须在循环外声明count变量并将其初始化为1.
在循环内部,您必须使用count变量来对数组中的元素进行处理(从count开始),然后再处理下一个count元素.因此,您将需要在第一个循环中进行第二个循环.
Seems to me like in your simple for next you need to multiply a count variable by 2.
You have to declare the count variable outside the loop and initialise it to 1.
Inside the loop you have to use the count variable to do you processing of the elements in the array starting from the count and for the next count elements. So you''ll need a second loop within the first loop.


var x = Convert.ToInt32(Math.Pow(2, a));
var result = new int[x];
for (var i = 0; i < x; i++)
    result[i] = x + i;


此代码可以帮助您
This Code may help you
for (int i = 0; i < arrayName.Length; i++)
        {
            for (int j = Convert.ToInt32(Math.Pow(2, i)); j < arrayName.Length; j++)
            {
                //write your Code here.
            }
        }



菲利普·斯图克(Philip Stuyck)提出了一个很好的主意.



Philip Stuyck gave very good Idea.


这篇关于for循环遍历数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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