| | |
2D array to 1D array vice versa??
Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Sep 2008
Posts: 23
Reputation:
Solved Threads: 0
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 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?
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?
•
•
Join Date: Sep 2008
Posts: 23
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Sep 2008
Posts: 23
Reputation:
Solved Threads: 0
Look at this example : http://www.automateexcel.com/2008/10...rix-to-vector/
•
•
Join Date: Dec 2002
Posts: 461
Reputation:
Solved Threads: 56
Do you really need a 1d array. Why not just put the info into the listbox. i.e.
VB.NET Syntax (Toggle Plain Text)
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)) Next Next 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 Next Next End Sub End Class
Last edited by waynespangler; Nov 9th, 2008 at 1:38 pm.
Wayne
It is hard to understand how a cemetery can raise its burial rates and blame it on the cost of living.
It is hard to understand how a cemetery can raise its burial rates and blame it on the cost of living.
•
•
Join Date: Sep 2008
Posts: 23
Reputation:
Solved Threads: 0
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
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
![]() |
Similar Threads
- Pass a 2D-array with AJAX (JavaScript / DHTML / AJAX)
- How can i use the Bit manipulation in vb.net (VB.NET)
- deleting array elements (C)
- Impementing First GUI into Current App (Java)
- CS First Year student, help with arrays (C++)
- Doubts in i/o and others in 'C' (C++)
- Output 2D array? (C++)
- Graphics In Pixel: Part II (C++)
Other Threads in the VB.NET Forum
- Previous Thread: data table
- Next Thread: vb.net Fill function
| Thread Tools | Search this Thread |
.net .net2008 2005 2008 access account array basic beginner bing browser button buttons center check client code crystalreport cuesent data database datagrid datagridview date datetimepicker designer dissertation dissertations dissertationtopic dropdownlist eclipse excel fade file-dialog filter forms ftp generatetags gridview html images input insert intel internet listview mobile monitor net objects open output panel passingparameters pdf picturebox port position print printing problem read remove save searchbox searchvb.net select serial settings shutdown soap sqlserver survey table tcp temperature text textbox timer timespan transparency trim update user vb vb.net vb.netformclosing()eventpictureboxmessagebox vb2008 vba vbnet visual visualbasic visualbasic.net visualstudio.net visualstudio2008 web winforms wpf wrapingcode year





