在数组中搜索值 [英] Searching arrays for values

查看:113
本文介绍了在数组中搜索值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧我正在为Chem开发一个VB项目,我正在制作它,用于将克转换为摩尔,摩尔转换为分子或任何类似的东西。我有大约110个元素设置和工作正常我这样做但我有一种感觉这是一种可怕的方式和丰富。


我有一个文本框设置如果我输入元素He并点击输入它会发现ChemElement(1)=" He"并调用

Sub DisplayElement(ByVal number As Decimal,ByRef label2 As Label)"

然后将化学质量显示在标签中。


所以基本上这里是我的代码

alrighty i am working on a VB project for Chem and im making it for converting grams to moles, moles to molecules or any thing like that. and i have about 110 elements set up and working fine the way that im doing it but i have a feeling this is a horrible way and to abundant.

i have a textbox set up to if i type in the element He and hit enter it will find ChemElement(1)="He" and it calls
"Sub DisplayElement(ByVal number As Decimal, ByRef label2 As Label)"
Which then displays the Chemical mass into a Label.

so basically here is my code

展开 | 选择 | Wrap | 行号

推荐答案

使用foreach循环。使用foreach或for循环检查使用数组内部的值输入的值并进行处理
use a foreach loop. Check the value entered with the values inside the array using foreach or for loop and do the processing


我不熟悉那些但我正在编程2课程通过我的highschool在中查找教科书并找到了一些信息,但仍然有点迷雾,


如何使用我的字符串/数组。


dim ChemElement(109)as string((或数组不确定使用哪一个))。


但使用IndexOf函数。这样做是否会更有效率?如果是这样,你可以帮助我使用基于我的代码的示例代码吗?
im not really familiar with those but im in programming 2 course through my highschool looked it up in the text book and found some information but still a lil foggy about it,

how about using my string/array.

dim ChemElement(109) as string ((or array not sure which one to use)).

but using the IndexOf function. would it be more efficient to do it that way or not? and if so could you help me with a example code based off of my codes uptop?


除非你正在处理一个特别大的数组 - 一个足够快的方法是使用for循环。这使您能够进行不区分大小写的搜索,并且可能会在鼠标按钮完全结束之前返回您的答案。


这个简单的函数将返回第一个匹配的索引数组中的项,如果未找到任何内容,则返回-1。它还假设你的数组是一个字符串数组......

Unless you are dealing with a particularly large array - a quick enough method is to use a for loop. This gives you the ability to do a case insensitive search and will likely return your answer before the mouse button is all the way up.

This simple function will return the index of the first matching item in the array, or -1 if nothing was found. It also assumes your array is an array of strings...

展开 | 选择 | Wrap | 行号


这篇关于在数组中搜索值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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