C#,递归循环? [英] C#, Recursive loops?

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

问题描述

我有一个列表数组.
现在我需要遍历此数组并填写职位,
取决于级别.
每个级别将从位置10开始,增加10.
数组按级别排序.可能无法更改.
我听说过有关递归循环的内容,但是我对这些并不熟悉.
下面是开始位置.

I have a list array.
Now i need to go through this array and fill in positions,
depending on the level.
Each level will start at position 10, increasements are 10.
The array is sorted at Level. This may not be changed.
I heared something about recursive loops, but i am not familiar with these.
Below is the start position.

Level|Position|ItemGroup|I
1    |        |PCBFLX   |401246900|
1    |        |CASCER   |402984000|
1    |        |CASCER   |402961200|
1    |        |MPCAH    |401617802|S
2    |        |CASCER   |400094000|
2    |        |CASCER   |402961600|
2    |        |CASCER   |402961000|
2    |        |CASCER   |402965000|
2    |        |CASCER   |402965100|
2    |        |CASCER   |402976900|
2    |        |CASCER   |402965200|
2    |        |DISDIO   |400568400|
1    |        |CASCER   |402961200|
1    |        |CASCER   |402961200|
2    |        |CASCER   |402965200|
3    |        |CASCER   |402965200|
3    |        |CASCER   |402965200|
1    |        |CASCER   |402965200|




结果应为:

需要这样做''递归''吗?
当级别更改的编号应从上一个级别(相同级别)开始或继续时




Result should be:

Need this to be done '' recursive ''?
When level changed numbering should start or to be continued from previous level (same level)

Level|Position|ItemGroup|I
1    |   10   |PCBFLX   |401246900|' Start 10, first item in array lev1
1    |   20   |CASCER   |402984000|' +10
1    |   30   |CASCER   |402961200|' +10
1    |   40   |MPCAH    |401617802|' +10
2    |   10   |CASCER   |400094000|' Start 10, items belong to previous 
2    |   20   |CASCER   |402961600|' +10
2    |   30   |CASCER   |402961000|' +10
2    |   40   |CASCER   |402965000|' +10
2    |   50   |CASCER   |402965100|' +10
2    |   60   |CASCER   |402976900|' +10
2    |   70   |CASCER   |402965200|' +10
2    |   80   |DISDIO   |400568400|' +10
1    |   50   |CASCER   |402961200|' See level 1, pos. 40 '+10
1    |   60   |CASCER   |402961200|' +10
2    |   10   |CASCER   |402965200|' Start 10,items belong to previous 
2    |   20   |CASCER   |402965200|' +10
3    |   10   |CASCER   |402965200|' Start 10, items belong to previous 
3    |   20   |CASCER   |402965200|' +10
1    |   70   |CASCER   |402965200|' See level 1, pos. 60 '+10





Somebody have an idea?

推荐答案

递归循环只是一个自我调用的函数.无需在这里使用一个.

执行此操作的一种方法是使用某种形式的HashMap(一个集合),其中级别是键,而最后使用的位置是关联级别的值.

逐步检查数组,检查哈希图的级别和最后一个位置值(如果存在),添加10,更新数组和哈希图,然后继续.如果不存在,则将其添加到哈希图中,并将其值设置为10,然后继续.
A recursive loop is just a function that calls itself. No need to use one here.

One method of performing this is to use a some form of HashMap (a collection) where the level is the key and the last used position is the value for the associated level.

Step through the array, check the hashmap for the level and the last position value, if exists add 10, and update the array and the hashmap, then move on. If it doesn''t exists, add it to the hashmap and set the value to 10, and move on.


在具有数据层次结构时,递归非常有用.

可能是遍历树或从父子关系中获取数据.在您的情况下,这非常简单,因此我建议不要进行递归,而应使用普通循环来做到这一点.

如果您想了解递归,可以去:
http://www.freenetpages.co.uk/hp/alan.gauld/tutrecur.ht [ ^ ]

:rose:
Recursion is useful when you have a hierarchy of data.

May be a Tree Traversal or fetching data from parent-child relation. In your case it is very straight forward, so I recommend not to go for recursion, rather use normal loop to do this.

If you want to know about Recursion you can go :
http://www.freenetpages.co.uk/hp/alan.gauld/tutrecur.ht[^]

:rose:


我找到了一个正常的循环!

I found a normal loop to do so!

<pre lang="cs">private void setPos()
        {
            recBom Bom;
            int position = 0;

            for (int level = 1; level < 10; level++)
            {
                for (int i = 0; i < arrBom.Count; i++)
                {
                    Bom = arrBom[i];
                    if (Bom.Level > level) continue;

                    if (Bom.Level == level)
                    {
                        position += 10;
                        Bom.Position = position.ToString();
                    }
                    else
                    {
                        position = 0;

                    }
                }
            }



这篇关于C#,递归循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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