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

8 Years
Discussion Span
Last Post by __avd

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:

so in a 3x3 position (1,1)
The thing i now need to write a algorithm to change the index from 2D to 1D and one to be the opposite.


Do you really need a 1d array. Why not just put the info into the listbox. i.e.

Public Class Form1
    Dim ary(4, 3)

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        For col = 0 To 3
            For row = 0 To 4
                ListBox1.Items.Add(ary(row, col))
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim index As Integer = 0
        For col As Integer = 0 To 3
            For row As Integer = 0 To 4
                ary(row, col) = index
                index += 1

    End Sub
End Class
Votes + Comments
fantastic help

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


Hi again,
Using the above code how could i get the listbox to obtain the corresponding 2D array index.


>Can the same algorithm be used in C++?

Yes. If you have any question, start your own thread.

Thread closed.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.