Lets say I have an array of chars which I have inputted from in console. How can I write them into two-dimensional array in the following way:
1 6 11 16 21
2 7 12 17 22
3 8 13 18 23
4 9 14 19 24
5 10 15 20 25
Or would it be better to use Lists?
Thank you!
Assuming you have the inputs in a single-dimensional array of length 25:
You can create a two-dimensional array that stores the data in a 5×5 grid using the
Array2D.initfunction:The function initializes the array using the specified size (first two parameters) and calls the provided function to fill the value for every element of the array. By accessing the element at index
j*5 + i, you get the structure that you wanted in your sample.EDIT You cannot append a row to an array (array has a fixed size), but you can easily create a larger array and copy data there: