Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
Hi, I want to fill 2d array using for loop, I want to enter 9 rows in 2d array with 3 record in each row my code is something like this, I want test array should contain
9 rows with 3 elements in each row. In case of this code no new row has been added to the array rather first row is updated each time
 
for (int i = 0; i < 9; ++i)
            {
                test[i] = 1;
                // arr[i] = [9]{1,2,3};
                //test[i]={1,2,3};
                for (j = 0; j < 2; ++j)
                {
                    int[][] test = new int[] { 1, 2, 3 };
                }
            }
Posted 7-Jan-13 18:36pm
Edited 7-Jan-13 19:25pm
v4
Comments
PIEBALDconsult at 7-Jan-13 23:48pm
   
So what's the problem? And why are you unsure what your code is?
ramesh0285 at 7-Jan-13 23:54pm
   
i am not getting this can u suggest me code its not adding new row, only updating first row with new value instead of adding second one.
ramesh0285 at 8-Jan-13 0:32am
   
in above simple code, in my original code element of 2d array will be value of pixels in an image so finally my 2d array would contain 1024 rows with 9 element in each row. plz help me this is college assignment.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You want to do two loops, nested.
 
for(int i =0; i < 3; ++i)
{
for(int j =0; j < 3; ++j)
{
  // add items here
}
}
  Permalink  
Comments
ridoy at 8-Jan-13 1:06am
   
+5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

check this
 
 
            //declare new 2d array
            int[,] test=new int[9,3];
 
            for (int i = 0; i < 9; ++i)
            {
                for (int j = 0; j < 3; ++j)
                {
 
                    //setting value for each element
                    test[i, j] = 1;
                }
            }
 
           //writing the elements in console window

            foreach (int i in test)
            {
               // MessageBox.Show(i.ToString());
                Console.WriteLine(i);
            }
        
  Permalink  
Comments
ridoy at 8-Jan-13 1:06am
   
+5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Other then using two for-loops, if you have a small 2-D array, you can do it at the time of defining array itself.
Example:
string[,] arr = new string[,]
    {
        {"A", "F"},
        {"B", "G"},
        {"C", "H"},
        {"D", "I"},
        {"E", "J"}
    };
Refer: 2D Array[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

 for(i=0 ; i<now_of_rows; i++ )
 {
   for(j=0 ; j<now_of_cols ; j++)
   {
     arr[i][j] = //assign your value here
   }
 }
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 406
1 Marcin Kozub 225
2 Sergey Alexandrovich Kryukov 205
3 /\jmot 175
4 Shweta N Mishra 161
0 OriginalGriff 8,289
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,624
3 Maciej Los 4,989
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 8 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100