Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I would like to populate an array with an array of values

I have 5 array lists each which are 100 elements long (4 for the input and 1 for the output
How do I populate the multidimensional arrays?


C#
\
INPUT = new double[4][];
IDEAL = new double[1][];

for (int i = 0; i < 100;i++)
{
     ???
}
Posted
Comments
Sergey Alexandrovich Kryukov 13-Oct-13 20:35pm    
Those are not array lists, but arrays. You should not use ArrayList as this class is obsolete.
—SA
BillWoodruff 14-Oct-13 0:45am    
There is the outside possibility that the OP really means what they say: that is, they have ArrayLists now, and want to convert them into Arrays. This is a good example of a question that should not be judged until there's a response to a request for clarification.
Sergey Alexandrovich Kryukov 14-Oct-13 1:24am    
Right. Way too often we see the answers which should not be given before an inquirer clarifies the question.
—SA
BillWoodruff 13-Oct-13 23:35pm    
Are you saying that you have five separate ArrayLists defined now, each with #100 elements, and those ArrayLists are initialized and "filled" with data ?

And what you want is to create two Arrays, and move all the Data from the ArrayLists into the Arrays ?

1 solution

If you want to do it with for-loops, then you will need so much loops as dimensions in the array.
i.e. (taking your code)
C#
INPUT = new double [4][100]
IDEAL = new double [] // If there is only one array outputs here you don't need the [1]

for (int i = 0; i < 4; i++)
{
   for int j = 0; i < 100; i++)
   {
      INPUT [i][j] = i+(j*2);
   }
}

// that will feed your first array with 0,2,4,6,8,10,...,200, 
// the second with 1,3,5,7,9,...,201, the third with 2,4,6,8,10,...202 and 
// the fourth with 3,5,7,9,11,...,203


In conclusion if you cascade loops, you go through the different dimensions of your array.

If you start complicating it, you might think about having classes or using lists, maps or even a database.
 
Share this answer
 
v2

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