469,356 Members | 2,626 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,356 developers. It's quick & easy.

Merging Two 1D Arrays into One

5
Good Afternoon All, I am working with Visual Studio.Net 2003

If I have Two 1D Arrays:

Dim intX() As Integer = {1, 3, 5, 7, 9, 11, 13, 15, 17}
Dim intY() As Integer = {2, 4, 6, 8, 9, 10, 12, 14, 16}
Dim intZ(17) As Long

and need to merge array intX with array intY to form a new array intZ, is this possible?????

Any Help would be appreciated, Thanx In Adavance!

SSG A
Go Army
May 12 '07 #1
5 1516
Killer42
8,435 Expert 8TB
If I have Two 1D Arrays:

Dim intX() As Integer = {1, 3, 5, 7, 9, 11, 13, 15, 17}
Dim intY() As Integer = {2, 4, 6, 8, 9, 10, 12, 14, 16}
Dim intZ(17) As Long
and need to merge array intX with array intY to form a new array intZ, is this possible?????
The simplest way would be two For loops, something like...
Expand|Select|Wrap|Line Numbers
  1. Dim I As Long
  2. For I = 0 To 8
  3.   intZ(I) = intX(I)
  4. Next
  5. For I = 0 To 8
  6.   intZ(I+9) = intY(I)
  7. Next
Of course, you could just do both the assignments within the one loop if you prefer.

If you need the new list sorted, it's probably simplest to just sort inZ() after joining them.
May 13 '07 #2
Sarge
5
The simplest way would be two For loops, something like...
Expand|Select|Wrap|Line Numbers
  1. Dim I As Long
  2. For I = 0 To 8
  3.   intZ(I) = intX(I)
  4. Next
  5. For I = 0 To 8
  6.   intZ(I+9) = intY(I)
  7. Next
Of course, you could just do both the assignments within the one loop if you prefer.

If you need the new list sorted, it's probably simplest to just sort inZ() after joining them.
Thanx for the reply Killer42, I tried the code and I get an error of outside bound os the array, here is the whole thing I am trying to do:

Dim intX() As String = {4, 9, 12, 15, 22, 33, 44, 66, 72, 84, 87, 92, 96, 98, 99}
Dim intY() As String = {6, 8, 12, 16, 24, 31, 68, 71, 73, 74, 81, 93, 94}
Dim intZ(27) As String

I am wanting to merge the two arrays intX and intY into intZ and place the output to a messagebox that has the two merge arrays as so:

4, 6, 8, 9, 12, 12, 15, 16, 22, 24, 31, 33, 44, 66...................etc.

so intX has 15 elements, intY has 13 elements and after they merge into intZ, intZ will have 28 elements.

Any Help Killer42 would be appreciated, thanx
May 13 '07 #3
Killer42
8,435 Expert 8TB
Thanx for the reply Killer42, I tried the code and I get an error of outside bound os the array...
The technique I demonstrated is very simple and will certainly work, so I'd guess there must have been some sort of bug in the implementation. At a guess, perhaps you tried to copy my code too precisely. I note that in the latest example your input arrays are different sizes, while my code assumed they were the same size.

In any case, show us the actual code you used, and let's see if we can beat the problem.

P.S. Personally, I never allow my arrays to start at (0). It causes too much confusion, because humans just don't think that way. I don't know what the equivalent would be in VB.Net, but in VB6 I always define them in the form intX(1 To 15) to ensure that I know exactly what I'm getting.
May 13 '07 #4
Sarge
5
Killer42, thanx for the response:

Here is what I am wanting to do merge intX and intY into intZ and show the result in a messagebox:


Dim intX() As String = {4, 9, 12, 15, 22, 33, 44, 66, 72, 84, 87, 92, 96, 98, 99}
Dim intY() As String = {6, 8, 12, 16, 24, 31, 68, 71, 73, 74, 81, 93, 94}
Dim intZ(27) As String

I am wanting to merge the two arrays intX and intY into intZ and place the output to a messagebox that has the two merge arrays as so:

4, 6, 8, 9, 12, 12, 15, 16, 22, 24, 31, 33, 44, 66...................etc.

I know in the project I have included a TextBox for output just to see it work but I will figure that out as soon as I can get this to work

Expand|Select|Wrap|Line Numbers
  1. Dim intX() As Integer = {"4", "9", "12", "15", "22", "33", "44", "66", "72", "84", "87", "92", "96", "98", "99"}
  2.     Dim intY() As String = {"6", "8", "12", "16", "24", "31", "68", "71", "73", "74", "81", "93", "94"}
  3.     Dim intZ(26) As String
  4.  
  5.  
  6.  
  7.  
  8.     Private Sub btnMerge_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMerge.Click
  9.  
  10.  
  11.         Dim I As Long
  12.  
  13.  
  14.  
  15.         For I = 0 To 14
  16.             intZ(I) = intX(I)
  17.  
  18.         Next
  19.  
  20.         For I = 0 To 12
  21.             intZ(I + 12) = intY(I)
  22.             txtBox1.Text = txtBox1.Text & intZ(I)
  23.  
  24.  
  25.        Next 
when I merge the arrays not all of it shows in the message box, I was trying to find an attachment feature to upload the zipped file to you, I guess they dont allow that.

Thanx for the help
Sarge
May 14 '07 #5
Killer42
8,435 Expert 8TB
Your code only moves the first 13 occurrences of the array into the textbox

I believe your first loop is OK. The second loop looks as though it has two problems.
  1. intZ(I + 12) = intY(I)
    Where did the +12 come from? Shouldn't this be +13 or +14? (I hate zero-based arrays.)
  2. txtBox1.Text = txtBox1.Text & intZ(I)
    This looks OK, but as I said, it's only being applied to the first 13 elements
May 15 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Tmenke | last post: by
4 posts views Thread by Bush will disarm all workers next | last post: by
3 posts views Thread by Patrick | last post: by
3 posts views Thread by André Hänsel | last post: by
8 posts views Thread by vidishasharma | last post: by
5 posts views Thread by John | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.