Click here to Skip to main content
15,885,998 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi to all I'm new in C# and programming and my problem is to convert a 2d-array into a
List<int[]>
.
My goal is to store in list items the rows from the array. What I've tried so far is the above
C#
public static List<int[]> GetMyNumbers(int[,] result)
       {

           int[,] res = result;

           int length = res.GetUpperBound(0) + 1;
           int width = res.GetUpperBound(1) + 1;
           int[] rows = new int[width];
           List<int[]> numberArrays = new List<int[]>();

           for (int i = 0; i < result.Length / width; i++)
           {
               for (int k = 0; k < width; k++)
               {
                   rows[k] = res[i, k];
                   Console.Write(rows[k]);
               }
               Console.WriteLine();
               numberArrays.Add(rows);//this doesn't fill the list properly..It adds to all list items the last row of the 2d array

           }

           return numberArrays;


       }

The result array is the following..
C#
int[,]result={{ 2,3,7 }, {2,3,5 } ,{2,3,6},{ 2,4,7 }, {2,4,5 } ,{2,4,6}};

I have to mention here that the above array could change both in items and size, so I'm looking for some solution that would fit in any array size.
Thanks in advance for both your time and your help!!!
Posted

Try this:

C#
int[,] result = {{ 2,3,7 }, {2,3,5 } ,{2,3,6},{ 2,4,7 }, {2,4,5 } ,{2,4,6}};
List<int[]> numberArrays = new List<int[]>();
for (int i = 0; i < result.GetLength(0); i++)
{
    int[] temp = new int[result.GetLength(1)];
    for (int n = 0; n < temp.Length; n++)
    {
        temp[n] = result[i, n];
    }
    
    numberArrays.Add(temp);
}
 
Share this answer
 
v2
Comments
katerinaPapathak 2-Nov-11 7:44am    
thank you very much!!!!that worked fine!!!!could you point out what i did wrong??
just for further knowledge...thanks again for your time and solution..
LanFanNinja 2-Nov-11 8:19am    
Well it seems that rows is being passed to numberArrays.Add() by reference and by declaring int[] rows outside of the first for loop all references to rows inside of the numberArrays List was being updated with the new values. Screwy I know but that is whats happening. So by declaring rows (temp) inside the first for loop a new array is being created every iteration so the reference being passed to numberArrays is that of a new array and not the same one. All the rest of your code was fine.
katerinaPapathak 2-Nov-11 9:12am    
thank you once again!!!!
C#
for(int i=0;i<result.length;i++)>
{
   int[] newRow = new int[result[i]];
   for(j=0;j<result[i].length;j++)>
   {
      newRow[j] = result[i,j];
   }
   list.add(newRow);

}


mark as answer if solves your problem
 
Share this answer
 
v2
Comments
katerinaPapathak 2-Nov-11 6:43am    
have you tried running the above code?? the command int[] newRow=new int [result[i]]; throughs me error and at the begging of the loop tells to create an array with the size of the element 0 of result but the result is [,] array..could you explain the sequence??

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900