• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 250
  • Last Modified:

Adding arrays

How can I add two arraylist togther to make a single arraylist. I have an array list of 1240 reading and I want to add the second array that has 6 arraylist reading to the front end, how can I do this

Dim array1 as New arraylist
Dim array2 as new arraylist


0
cmdolcet
Asked:
cmdolcet
  • 6
  • 5
  • 3
2 Solutions
 
Jorge PaulinoIT Pro/DeveloperCommented:
You can use AddRange to add the array1 into array2

array2.AddRange(array1)
0
 
cmdolcetAuthor Commented:
what if i wanted to put arraylist in the upper 6  of arraylist 1 total 1240 and the bottom 6

0
 
Jorge PaulinoIT Pro/DeveloperCommented:
You can use InsertRange where you can define where to insert

array2.InsertRange(0, array1)
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
Jorge PaulinoIT Pro/DeveloperCommented:
0 is the position (index)
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I think you'd have to iterate over array2 and use the Insert() command:

        For i As Integer = 0 To array2.Count - 1
            array1.Insert(i, array2(i))
        Next
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Haha...InsertRange() is better...silly Intellisense!

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
(and silly Idle_Mind)
0
 
Jorge PaulinoIT Pro/DeveloperCommented:
LOL,
Idle_Mind you have to select the Intellisense to show all the available properties and methods and not only the common ones :)
0
 
cmdolcetAuthor Commented:
ok so if i understand this correctly then
array2.InsertRange(0, array1) would insert the second array onto the top of the first therefore this code would only work if i insert the second array on the top of the forst array. but lets say i wanted to insert the second array on the bottom i would use the following code
array2.InsertRange(1240, array1)

correct?
0
 
Jorge PaulinoIT Pro/DeveloperCommented:
If you can insert at the bottom you can use AddRange:
array2.AddRange(array1)
 
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
If you want to add array2 into array1 then you need to call the methods against array1:

    array1.InsertRange(0, array2) ' <-- add array2 to the top of array1

    array1.AddRange(array2) ' <-- add array2 to the bottom of array1
0
 
cmdolcetAuthor Commented:
no lets say i want to not add the array 2 to the top but now the bottom how can i do that?

The overall idea is having 3 arraylist, arraylist 1 would be the master array i want to add arraylist 2 to the top of arraylist1 and arrayllist 3 to the bottom of arraylist 1

se the visual pic would be this
arraylist2
arraylist1
arraylist3

I hope this helps sorry for being so confusing :)
0
 
Jorge PaulinoIT Pro/DeveloperCommented:
Should be this way then to have all values in the array1

array1.InsertRange(0, array2)
array1.AddRange(array3)

Open in new window

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
That would be:

    array1.InsertRange(0, array2) ' <-- add array2 to the top of array1
    array1.AddRange(array3) ' <-- add array3 to the bottom of array1

(assuming that you want to change the contents of array1)

If you want to keep all the arraylists intact, then create a new one:

    Dim array4 As New ArrayList
    array4.AddRange(array2)
    array4.AddRange(array1)
    array4.AddRange(array3)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

  • 6
  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now