Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dim myarr As New ArrayList
Dim myarr1 As New ArrayList
myarr=[1,2,3] //these values i am adding dynamically
myarr1=[2,2,3]

i want to display result [1,2][2,2][3,3] ....

Can anybody please provide me the code?
Posted
Updated 7-Mar-15 7:31am
v2
Comments
Sergey Alexandrovich Kryukov 7-Mar-15 13:24pm    
This is not called "compare". What have you tried so far?
[EDIT] I edited this comment after the inquirer fixed the question title — thanks.
—SA

It's not clear why creating so non-informative output, but this is your choice.

To start with, don't use obsolete ArrayList use System.Collections.Generic.List<>.
Use the property Count to determine the number of elements, find out the minimum between the to and see if one list is shorter, use the minimum count. In a loop for 0 to count − 1, do that extremely simple thing you called "display", whatever it is. If it has to be put in a string, better don't use string type immediately, but collect data in an instance of the mutable counterpart of System.String, System.Text.StringBuilder:
https://msdn.microsoft.com/en-us/library/6sh2ey19%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.text.stringbuilder%28v=vs.110%29.aspx[^].

Do whatever you want with other elements if the lists have different lengths.

—SA
 
Share this answer
 
Comments
Thomas Daniels 7-Mar-15 13:36pm    
+5
Sergey Alexandrovich Kryukov 7-Mar-15 13:41pm    
Thank you.
—SA
This code should do what you want:
VB.NET
Dim sb As New StringBuilder()
For i As Integer = 0 To CType(Math.Min(myarr.Count, myarr1.Count), Integer) - 1
    sb.AppendFormat("[{0},{1}]", myarr(i), myarr1(i))
Next
Dim output As String = sb.ToString()

Be sure to add Imports System.Text at the top of your code file.

The code uses a StringBuilder[^] to generate the output. The For loop counts to the minimal length of the two ArrayLists.
 
Share this answer
 
v4
Comments
Sergey Alexandrovich Kryukov 7-Mar-15 13:43pm    
5ed.
—SA
Thomas Daniels 7-Mar-15 13:46pm    
Thanks! I also updated my answer to count to the minimal length, as per your suggestion.
Sergey Alexandrovich Kryukov 7-Mar-15 13:51pm    
Great. One little problem is that Math.Min returns double, so you would need to add typecast for other languages; I usually code integer min.
—SA
Thomas Daniels 7-Mar-15 14:28pm    
Good point. I added a CType cast.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900