## Arrays and Collections
---
### **Arrays**
An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed. In a simple way an array is just variable that can contain multiple values unlike a simple variable.
Syntax :
```
Dim name_of_variable As DataType = {value1, value2, ...}
```
Example :
```vbnet
Dim x() As Integer = {4, 5 , 15, 95}
Dim students() As String = {"Marie", "Peter", "Helene"}
' Declare an array but initialize it later
Dim countries(2) As String
countries(0) = "Italy"
countries(1) = "Germany"
countries(2) = "France"
```
We can not print out the array in *Console.WriteLine()* or *Console.Write()* as we do for simple variable.
```vbnet
Console.WriteLine(students)
```
output:
```
System.String[]
```
It doesn't display elements stored in array but just display the data type of the array.
So to access any element of the array we must specify the position (index) of the element. for example :
```vbnet
students(2)
```
output :
```
Helene
```
We start counting the index from 0
```vbnet
x(0)
```
output :
```
4
```
But can show all the elements in array by using a loop
__*Using For Loop :*__
```vbnet
For i As Integer = 0 To 2
Console.WriteLine(students(i))
Next
```
output :
```
Marie
Peter
Helene
```
Remember that *i* will be counting from 0 to 2
```
students(0) = Marie
students(1) = Peter
students(2) = Helene
```
__*Using Do Loop :*__
```vbnet
Dim i As Integer = 0
Do While (i < 3)
Console.WriteLine(students(i))
i += 1
Loop
```
output :
```
Marie
Peter
Helene
```
__*Using For each Loop :*__
In the precious section we didn't talk about **For each loop**, because this loop is only used on arrays and collection.
Syntax :
```
For Each element [ As datatype ] In group
[ statements ]
[ Continue For ]
[ statements ]
[ Exit For ]
[ statements ]
Next [ element ]
```
```vbnet
For Each studentName As String In students
Console.WriteLine(studentName)
Next
```
output :
```
Marie
Peter
Helene
```
**Properties of The Array**
Name | Description
-----|------------
IsFixedSize |Gets a value indicating whether the Array has a fixed size.
IsReadOnly |Gets a value indicating whether the Array is read-only
Length | Gets the total number of elements in all the dimensions of the Array.
Long Length | Gets a 64-bit integer that represents the total number of elements in all the dimensions of the Array.
Rank | Gets the rank (number of dimensions) of the Array. For example, a one-dimensional array returns 1, a two-dimensional array returns 2, and so on.
SyncRoot|Gets an object that can be used to synchronize access to the Array.
IsSynchronized|Gets a value indicating whether access to the Array is synchronized (thread safe).
Go [here](https://msdn.microsoft.com/en-us/library/system.array(v=vs.110).aspx) for more details.
Let try some properties
```vbnet
Console.WriteLine("IsFixedSize : " & students.IsFixedSize)
Console.WriteLine("IsReadOnly : " & students.IsReadOnly)
Console.WriteLine("Length : " & students.Length)
Console.WriteLine("IsSynchronized : " & students.IsSynchronized)
```
output :
```
IsFixedSize : True
IsReadOnly : False
Length : 3
IsSynchronized : False
```
**Methods :**
Name | Description
----- | ---------
BinarySearch(Array, Object) | Searches an entire one-dimensional sorted array for a specific element, using the IComparable interface implemented by each element of the array and by the specified object.
Clear(Array, Int32, Int32) | Sets a range of elements in an array to the default value of each element type.
ConstrainedCopy(Array, Int32, Array, Int32, Int32) |Copies a range of elements from an Array starting at the specified source index and pastes them to another Array starting at the specified destination index. Guarantees that all changes are undone if the copy does not succeed completely.
ConvertAll<TInput, TOutput>(TInput[], Converter<TInput, TOutput>) | Converts an array of one type to an array of another type.
Copy(Array, Array, Int32)|Copies a range of elements from an Array starting at the first element and pastes them into another Array starting at the first element. The length is specified as a 32-bit integer.
IndexOf(Array, Object) |Searches for the specified object and returns the index of its first occurrence in a one-dimensional array.
SetValue(Object, Int32)|Sets a value to the element at the specified position in the one-dimensional Array. The index is specified as a 32-bit integer.
Sort(Array) | Sorts the elements in an entire one-dimensional Array using the IComparable implementation of each element of the Array.
Go [here](https://msdn.microsoft.com/en-us/library/system.array(v=vs.110).aspx) for more details.
example :
Sorting an array
```vbnet
Dim x() As Integer = {8, 41, 0, 74, -1, 4, 25, 452}
Array.Sort(x)
For Each y As Integer In x
Console.WriteLine(y)
Next
```
output :
```
-1
0
4
8
25
41
74
452
```
Get the sum of elements in an array
```vbnet
Dim x() As Integer = {8, 40}
Console.WriteLine(x.Sum())
```
output :
```
48
```
Get the smallest and biggest elements in an array
```vbnet
Dim x() As Integer = {8, 41, 0, 74, -1, 4, 452, 12}
Console.WriteLine("Smallest : " & x.Min())
Console.WriteLine("Biggest : " & x.Max())
```
output :
```
Smallest : -1
Biggest : 452
```
__*Modify elements in an Array :*__
We gonna now try to add or modify elements in an array after it has been declare.
Has we saw previously for accessing an element in array we must specify the index of the element, it's also the case here
```vbnet
Dim x() As Integer = {8, 41, 0, 74, -1, 4, 25, 452}
' Modify the element at index 2 of x
x(2) = 10
Console.WriteLine(x(2))
```
output :
```
10
```
In the above example we modified the element at index 2 that was *0* we changed it to *10*
__*Searching in an array :*__
Other things we can do is searching an element in array.
*Using Contains method*
```vbnet
Dim x() As Integer = {8, 41, 0, 74, -1, 4, 25, 452}
' Searching for 410
Console.WriteLine(x.Contains(410))
' Searching for -1
Console.WriteLine(x.Contains(-1))
```
output :
```
False
True
```
The above method only search one elements in an array, let try to create something to will search for more then 1 element.
```vbnet
' This code, will search for element containing in both x and y
Dim x() As Integer = {20, 85, 62, 41}
Dim y() As Integer = {93, -5, 20, 85}
For Each elementX As Integer In x
' Search elementX in y
For Each elementY As Integer In y
If elementX = elementY Then
Console.WriteLine(elementX & " is found in x and y")
End If
Next
Next
```
output :
```
20 is found in x and y
85 is found in x and y
```
__*Multi dimensional Array*__:
A multi dimensional array is a array that contain other array in it.
```vbnet
' This is a two dimensional array, all elements in the students are arrays.
Dim students(,) = {{"Robert", 17}, {"Alan", 28}, {"Milan", 10}}
```
Accessing element in multi dimensional array
```vbnet
Console.WriteLine(students(0, 0))
Console.WriteLine(students(0, 1))
```
output :
```
Robert
17
```
Or can also declare a multidimensional array as follow :
```vbnet
' this array is populated by 3 arrays
Dim students()() = {New String() {"Robert", 17}, New String() {"Alan", 28}, New String() {"Milan", 10}}
```
```vbnet
'This select the first array in students
Console.WriteLine(students(0))
```
output :
```
System.String[]
```
```vbnet
'This select the second element in the first array of students
Console.WriteLine(students(0)(1))
```
output :
```
17
```
Iterating over the a multi dimensional array
```vbnet
Dim students()() =
没有合适的资源?快使用搜索试试~ 我知道了~
LEARN-VISUAL-BASIC 跟我一起来学VisualBasic源码一份
共25个文件
md:17个
png:8个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 201 浏览量
2024-03-17
11:05:21
上传
评论
收藏 496KB ZIP 举报
温馨提示
其中包括了变量、函数、运算符、条件、从用户获取值、示例、循环、数组、字符串、日期和时间等等,其中一共有 10 个示例,欢迎取用.
资源推荐
资源详情
资源评论
收起资源包目录
LEARN-VISUAL-BASIC.zip (25个子文件)
LEARN-VISUAL-BASIC
basic
9 Date and time
readme.md 3KB
4 get value from user
readme.md 1KB
7 arrays
examples.md 4KB
readme.md 10KB
3 condition
readme.md 2KB
Intro
readme.md 967B
2 operator
answers.md 64B
readme.md 2KB
exercices.md 1KB
10 functions
readme.md 5KB
1 variables
answers.md 348B
exercices.md 410B
README.md 5KB
8 Strings
readme.md 3KB
6 Loops
readme.md 5KB
5 examples
readme.md 2KB
exercices.md 3KB
img
1.PNG 7KB
v3.png 140KB
2.PNG 7KB
5.PNG 7KB
v2.png 176KB
4.PNG 7KB
v1.png 148KB
3.PNG 6KB
共 25 条
- 1
资源评论
小白的杂货铺
- 粉丝: 253
- 资源: 324
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 电子设计项目毕业设计及产品设计资料开关电源论文资料
- 模型预测控制(MPC)基准测试问题的开放集合Open collection of model predictive contro
- 基于用户的协同过滤算法实现的商品推荐系统
- TI杯大奖赛本科组+研究生组论文+源代码+PCB自学习式走迷宫智能小车
- TI杯大奖赛本科组+研究生组论文+源代码+PCB专业组-仪器仪表类-兰州大学-基于MSP430的心电监控系统
- graphab-2.6英文用户手册
- 修正正弦波逆变器原理图
- pyltp-0.2.1-cp36+pyltp-0.4.0-cp36.zip
- TI杯大奖赛本科组+研究生组论文+源代码+PCB专业组-先进控制类-电子科技大学-基于TMS320F28027的新能源可控整流与
- 基于python的五子棋游戏编程代码
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功