Hey,
i am trying to convert a 2D array to a 1D array and also the reverse. Not sure if i am doing it right so does anyone know how it can be done. below is the code i have for 2D to 1D but now how do i do the reverse.
Dim Index As Integer
For j = 1 To array.GetLength(0)
For i = 0 To array.GetLength(0) - 1
Index = (i * rowNum) + j
Next i
Next j
Return Index
I don't see how this makes a 2D array in to a 1D array. All it does is loop through all the 'cells' of the array and stores a number as Index. What, by the way, is rowNum? It is neither declared nor set in the provided code.
I'm guessing what you will want to do is create a 1D array and store the values of the 2D array into the 1D array.
As far as creating a 2D array from a 1D array, you will have to provide additional details about the 1D array. Are all the items for the first row stored first and then the items for the second row? How do we know where the end of each row is?
Thanks for the reply Timo,
yes i basically want to find the coresponding index for a (row,col) in a 1D representation of 2D.
For e.g i a 3x3 array the element in positon (1,1) is 4 in a 1D array. I need to write funtions which can convert from 2D to 1D and next ID back to 2D because the 2D will be dislayed in a listbox as 1D. When i update an element in the listbox it should update the corresponding element stored in the index position in the matrix.
it is a bit much to explain hope you dont get lost.
here is how i did some calculation:
(rowIndex*no_Of_col)+colIndex
so in a 3x3 position (1,1)
1*3+1=4
The thing i now need to write a algorithm to change the index from 2D to 1D and one to be the opposite.
Thanks you,
i will have a look at you method and see if i could use it in any way. But i do need to display the 2D in a listbox as 1D array of strings. i also need to update the data in the listbox so i will need a reverse of the method. The object is to get the index of an element so if i could write a method which use the index of an element in the listbox and find is corresponding index in the 2D array and vice versa.
sometimes i am not even sure if i understand it my self. just started vb 2 months ago
Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.
This thread is more than three months old
No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.