0

From Reading up online, I've seen most answers to solving dynamic arrays such as to use a list. I'm a bit confused how to perform list operations on a multidimensional array. Maybe if I can understand how to implement a piece of code such as below, I'd be able to grasp them better.

    public void class Class1(){
    string[,] array;

    public void ArrFunction()
    {
        array=new string[rand1,rand2];
        int rand1=SomeRandNum;
        int rand2=SomeRandNum2;
        for(int i=0; i<rand1; i++){
           for(int j=0; j<rand2; j++){
              array[i][j]=i*j;
           }
        }
    }
4

1 回答 1

0

Your method would work fine, but you need to declare and initialize your rand1 and rand2 variables before using them, and access into your array correctly:

public void ArrFunction()
{
    int rand1=SomeRandNum;
    int rand2=SomeRandNum2;
    array=new string[rand1,rand2]; // Don't use these until they're set
    for(int i=0; i<rand1; i++){
       for(int j=0; j<rand2; j++){
          array[i, j]=i*j; // Use [i, j], not [i][j]
       }
    }
}

Also, you can't initialize an array to a string:

// This gets initialized in your method
string[,] array; // =""; 
于 2013-07-31T17:36:55.223 回答