Arrays: Variables That Represent More Than One Value
来源:互联网 发布:nginx ssl模块加载 编辑:程序博客网 时间:2024/04/29 03:22
In this lesson, you will learn how to use arrays to store groups of values.
As you learned in the previous lessons, variables are used to store different types of data for use by your program. There is another type of variable called an array that provides a convenient way to store several values of the same type.
For example, suppose you were writing a program for a baseball team and you wanted to store the names of all of the players on the field. You could create nine separate string variables, one for each player, or you could declare an array variable that looks something like the code below.
Dim players() As String
You declare an array variable by putting parentheses after the variable name. If you know how many values you need to store, you can also specify the size of the array in the declaration as follows.
Dim players(8) As String
You may find it odd that the size of the array is 8 when a baseball team has 9 players. This is because an array is made up of a number of values or elements, starting with element 0 and ending with the number specified in the declaration. In this case, the array contains the elements 0 through 8, for a total of nine elements.
Assigning Values to Arrays
As with other types of values, you need to assign values to arrays. To do so, you refer to the element number as part of the assignment, as shown below.
players(0) = "John"
players(3) = "Bart"
In the above code, the value John is assigned to the first element of the array (element 0) and the value Brett is assigned to the fourth element (element3). The elements of the array don't have to be assigned in order, and any unassigned element will have a default value—in this case, an empty string.
As with other types of values, you can declare and assign values to an array on a single line as follows.
Dim players() As Integer = {1, 2, 3, 4, 5, 6, 7, 8, 9}
In this case, curly brackets indicate a list of values. The values are assigned to elements in the order listed. Notice that size of the array isn't specified—it is determined by the number of items that you list.
Retrieving Values from Arrays
Just as you use numbers to specify an item's position in an array, you use the element number to specify which value you want to retrieve.
Dim AtBat As String
AtBat = players(3)
The above code retrieves the fourth element of the array and assigns it to the string variable AtBat.
Try It!
To store values in an array
On the File menu, choose New Project.
In the New Project dialog box, in the Templates pane, click Windows Application.
In the Name box, type MyFirstArray and then click OK.
A new Windows Forms project opens.
From the Toolbox, drag a Textbox control onto the form.
From the Toolbox, drag a Button control onto the form.
Double-click the Button to open the Code Editor.
In the Button1_Click event procedure, add the following code:
Dim players() As String = {"Dan", "Fred", "Bart", "Carlos", _
"Ty", "Juan", "Jay", "Sam", "Pedro"}
Dim i As Integer = CInt(Textbox1.Text)
MsgBox(players(i) & " is on first base.")
Notice that the above code uses the CInt function to convert the String value (TextBox1.Text) to an Integer (i). You can learn more about conversions in Closer Look: Converting from One Variable Type to Another.
Press F5 to run the program.
Type a number between 0 and 8 in the text box and click the button. The name corresponding to that element is displayed in a message box.
- Arrays: Variables That Represent More Than One Value
- The server time zone value '?????' more than one time zone
- How to limit an array of similar hashes to those that have more than one of the same key:value pair
- 解决“You have more than one commit that you are about to submit.”
- More than one order matters
- The server timezone value 'UTC' is unrecognized or represents more than one timezone
- Flume Error must not generate more than one output value per record field解决
- The server time zone value 'EDT' is unrecognized or represents more than one time zone.
- Mysql错误:The server time zone value is unrecognized or represents more than one time zone
- The server time zone value 'PDT' is unrecognized or represents more than one time zone
- The server timezone value 'xxx' is unrecognized or represents more than one time
- Read one Book more than one time
- 22.Identify the logical structure that will never have more than one data segment created for it.
- More than one global threading model defined
- more than one device and emulator
- Result consisted of more than one row
- more than one device and emulator
- more than one device and emulator
- Words and Text: Using String Variables to Organize Words
- 快速关机和重启
- Linux 共享库
- 几个简单的步骤大幅提高Oracle性能--我优化数据库的三板斧
- 使用import简化spring的配置文件
- Arrays: Variables That Represent More Than One Value
- 准备你的重大报告
- SAP Netweaver忠爱者
- 热门影集搭配互动游戏,网友成HBO活广告
- Arithmetic: Creating Expressions With Variables And Operators
- “软件工业奥斯卡”SYS-CON 读者选择奖: Java开发
- 过了一段混乱的日子
- 考研复习中...一切业务考完再说
- 我这半年,以及近期的安排