0

Consider I have a number of arrays as follows:

string[,] op1 = new string[9, 9];
string[,] op2 = new string[9, 9];
string[,] op3 = new string[9, 9];

How do I iterate over such arrays like the following?

int i;
int j;
for(i=0; i<3; i++) {
    // Iterate all the above three arrays here
}

I want to dynamically iterate all arrays starting with op by just changing the index.

I'm using C#.

4
  • If they are local variables, you cannot do that. If they are fields you can do that but you shouldn't. What's wrong with an array of arrays? Or a list of them? Commented Mar 21, 2014 at 8:54
  • Make an array or list of the arrays you want to iterate. Then iterate through the container array (loop within a loop). It sucks, but then you get what you asked for... Commented Mar 21, 2014 at 8:56
  • i know....but any alternate way of doing that?? Commented Mar 21, 2014 at 8:58
  • what's the context of your problem? Commented Mar 21, 2014 at 9:04

6 Answers 6

10

You can make an array on the fly and iterate:

string[,] op1 = new string[9, 9];
string[,] op2 = new string[9, 9];
string[,] op3 = new string[9, 9];

foreach (var item in new[] { op1, op2, op3 })
{
     //...
}
Sign up to request clarification or add additional context in comments.

Comments

2

You can get clever by writing a method using the params keyword which will automatically create an array of arrays for you.

To do that, you have to write an intermediary wrapper class for the arrays, because the params keyword can only be used with a single dimensional array.

I really only provide this code for the curious - you probably wouldn't really need to go to these lengths in real code. However, if you did find yourself often wanting to iterate over a set of two dimensional arrays, you can use this approach.

After writing the (reusable) helper classes your code to iterate the arrays would look like this:

string[,] op1 = new string[9, 9];
string[,] op2 = new string[9, 9];
string[,] op3 = new string[9, 9];

IterateArrays<string>(processArray, op1, op2, op3);

Where the processArray() method would be along these lines:

static void processArray(string[,] array, int index)
{
    Console.WriteLine("Processing array with index " + index);
}

Here's the full compilable example:

using System;

namespace ConsoleApp1
{
    public class ArrayWrapper<T>
    {
        public T[,] Array;

        public static implicit operator ArrayWrapper<T>(T[,] array)
        {
            return new ArrayWrapper<T> {Array = array};
        }
    }

    sealed class Program
    {
        void run()
        {
            string[,] op1 = new string[9, 9];
            string[,] op2 = new string[9, 9];
            string[,] op3 = new string[9, 9];

            IterateArrays<string>(processArray, op1, op2, op3);
        }

        static void processArray(string[,] array, int index)
        {
            Console.WriteLine("Processing array with index " + index);
        }

        public static void IterateArrays<T>(Action<T[,], int> action, params ArrayWrapper<T>[] arrays)
        {
            for (int i = 0; i < arrays.Length; ++i)
                action(arrays[i].Array, i);
        }

        static void Main(string[] args)
        {
            new Program().run();
        }
    }
}

Like I said, this is just to show how you can approach it. It'd just use @thumbmunkeys suggestion in real code.

Comments

1

You could create a list<string[,]> containing the string[,]'s

string[,] op1 = new string[9, 9];
string[,] op2 = new string[9, 9];
string[,] op3 = new string[9, 9];

//Create List, containing `string[,]`
List<string[,]> opList = new List<string[,]>();
//Add String[,]'s to list
opList.Add(op1);
opList.Add(op2);
opList.Add(op3);

//Loop over list
foreach(var itm in opList)
{
    //approach string[,]'s here
}

Comments

1
string[,] op1 = new string[9, 9];
string[,] op2 = new string[9, 9];
string[,] op3 = new string[9, 9];

List<string[,]> l = new List<string[,]>();

l.add(op1);
l.add(op2);
l.add(op3);

foreach(string[,] op in l)
{
 // iterate over op here
}

Or, if you don't want the additional lines to add the arrays to the list, you can:

List<string[,]> ops = new List<string[,]>{
    new string[9, 9];
    new string[9, 9];
    new string[9, 9];
}

foreach(string[,] op in ops)
{
 // iterate over op here
}

Comments

1

You can't do that. Easier would be to add the arrays to a List<T> and iterate the list to iterate the arrays:

List<string[,]> arrays = new List<string[,]>
{
    new string[9, 9],
    new string[9, 9],
    new string[9, 9]
};

foreach(var array in arrays)
{
    // Do something with the array...
}

Comments

-3

Use:

for(int i=0;i<9;i++)
{
    for(int j=0;j<9;i++)
    {                             
        // Iterate your string op1,op2,op3 for the desired result.    
    }
}

2 Comments

This is not an answer and it's not event correct too.
This makes no sense. He has arrays with different names, not a single array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.