Recursion in place of multiple nested for loops?

前端 未结 5 1605
日久生厌
日久生厌 2021-01-14 02:05

Im having some issues with trying to update a nested for loop to use recursion instead. Is it possible to access the a,b and c variables from the earlier for loops when usin

5条回答
  •  不思量自难忘°
    2021-01-14 02:44

    something like this will work:

    public void CreateIndexes(int a, int b, int c, Collection collection)
    {
        if(c == 10) {b++; c = 0;}
        if(b == 20) {a++; b = 0;}
        if(a == 10) return;
    
        int[] indexes = new int[3]{a,b,c}
        collection.add(indexes);
        c++;
    
        CreateIndexes(a, b, c, collection);
    }
    

提交回复
热议问题