Why arrays are using?

1 answer

Answer

1195491

2026-08-15 13:30

+ Follow

1. An array cuts down on the number of variables that need to be declared.

For example, if you wanted to store the numbers 1 to 10 without using an array you would literally have to declare ten separate variables.

Dim int1 as Integer = 1

Dim int2 as Integer = 2

Dim int3 as Integer = 3

Dim int4 as Integer = 4

Dim int5 as Integer = 5

Dim int6 as Integer = 6

Dim int7 as Integer = 7

Dim int8 as Integer = 8

Dim int9 as Integer = 9

Dim int10 as Integer = 10

Which is monstrously difficult to read and use further along in your code. A better way of doing it would be to use one variable; an integer array.

Dim intArray() As Integer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

Using this example you use the same variable throughout your code and access different parts of it as follows;

Dim intArray() As Integer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

Dim intTemp as Integer

MsgBox(intArray(0))

MsgBox(intArray(4))

This example displays two textboxes which will display the number 1 and 5 to the user (Note that array starts at zero)

2. An array can be used in itterative procedures such as a For... Next loop.

For example if we use the previous example of an integer array containing numbers 1 through 10, and with this array we want to display every number to the user.

Without an array this is an almighty block of code with a minimum of 20 lines, 10 declaring each variable and 10 outputting it to the user. With an array this can be reduced to four lines.

Dim intArray() As Integer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

For i = 0 to intArray.Length

MsgBox(intArray(i))

Next

The importance of this is that in our example we only have ten numbers stored, in the real world you could have hundreds. Declaring hundreds of variables and messing around with them will take up memory, obfuscate the code immensely and make it virtually unchangeable.

With the above example that array could be any length and contain a thousand variables and still function the same.

3. An array can be declared as a variable length.

Whilst it's all well good saying we have an example array containing numbers 1 through 10, it is unlikely to ever be that simple; we will always want to store an unknown amount of data at runtime.

Just declaring variables in the code wouldn't work for this because they would need to be hardcoded.

In this example we will pretend a form has been made with a listview containing a hundred items, when a user clicks a button we want to store everything they selected in the listview. (If you don't know what a listview is I would ask that question as well)

Dim intVarArray(ListView1.SelectedItems.Count - 1) as Integer

For i = 0 to ListView1.SelectedItems.Count - 1

intVarArray(i) = ListView1.SelectedItems(i).ToString

Next

To do this using nothing but variables would be near impossible as you wouldn't know how many items the user would select, as such you would have to declare a hundred variables and write hundreds of lines of code to do something that can be covered in four lines.

4. An array can be resized on the fly, either destroying or preserving existing data.

In the above example we created an array of variable length and then stored some data from a made up listview control, but it's possible when working in the real world we may need to add things to that variable.

We can Redim an array to change its length.

Dim intArray(9) as Integer

Redim intArray(14)

This declares a variable of an array of integers, sets the length to 10 and then redefines that array as an array of length 15. This will completely remake the variable destroying any data held within it.

Dim intArray(9) as Integer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

Redim Preserve intArray(14)

intArray(10) = 11

intArray(11) = 12

intArray(12) = 13

intArray(13) = 14

intArray(14) = 15

This declares a variable of length 10 and stores the number 1 to 10 inside it, it then redefines the variable as length 15 but preserves the data within it (It then adds the number 11 to 15 to that variable).

5. An array can have multiple dimensions.

Dim intArray(, ) as String = {{"White", "Rabbit"}, {"Brown", "Bear"}, {"Blue", "Water"}}

This declares something more unique than any other type of single variable, a multidimensional array, and stores some data within it.

--
Ac532 Answer:

Arrays can be disposed by using the .Dispose which means all allocated memory toward using the array will be lost meaning that your program will run faster.

ReportLike(0ShareFavorite

Copyright © 2026 eLLeNow.com All Rights Reserved.