如何索引显示两个arraylists [英] how to display two arraylists indexwise

查看:77
本文介绍了如何索引显示两个arraylists的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Dim myarr As New ArrayList

Dim myarr1 As New ArrayList

myarr = [1,2,3] //这些值我动态添加

myarr1 = [2,2,3]



i想显示结果[1,2] [2,2] [3,3] .. ..



有人可以提供给我代码吗?

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?

推荐答案

目前尚不清楚为什么要这么做 - 信息输出,但这是你的选择。



首先,不要使用过时的 ArrayList 使用 System.Collections.Generic.List<>

使用属性 Count 来确定元素的数量,找出to之间的最小值,并查看一个列表是否更短,使用最小计数。在一个0到 count - 1 的循环中,做一个非常简单的事情你称之为显示,无论它是什么。如果必须将其放入字符串中,最好不要立即使用字符串类型,而是在 System.String ,<$ c的可变对应实例中收集数据$ c> 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 [ ^ ]。



做你想做的事如果列表有不同的长度,则使用其他元素。



-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


这个代码应该做你想要的:

This code should do what you want:
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()



请务必在代码文件的顶部添加 Imports System.Text



代码使用 StringBuilder [ ^ ]生成输出。 For循环计数到两个ArrayLists的最小长度。


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.


这篇关于如何索引显示两个arraylists的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆