ARRAYS IN VB NET OBJECTIVES Introduction to array

ARRAYS IN VB. NET

OBJECTIVES Introduction to array Type of array Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional array Dynamic array Multidimensional array Jagged array

ARRAYS An array is a group of variables that have the same name and data type All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element

ARRAYS (CONTINUED) The most commonly used arrays are one-dimensional and two-dimensional Programmers use arrays to store related data in the internal memory of the computer

Dimensions The dimensionality of an array refers to the number of subscripts used to identify an individual element. Types of Array: There are following types of an array in VB. NET. Single Dimensional Array: Multi-Dimensional array: Such can be of two types. Rectangular Array Jagged Array

ONE-DIMENSIONAL ARRAYS A one-dimensional array is simply a row (or column) of variables Each element in an array is identified by a subscript, which Visual Basic. NET assigns to the variable when the array is created

ARRAYS STRUCTURE

ONE-DIMENSIONAL ARRAYS You refer to each variable in an array by its name and the variable’s subscript Names of the variables in a one-dimensional array named states

ONE-DIMENSIONAL ARRAYS (CONTINUED) Examples of declaring an array Dim cities(3) As String Dim int. Data(30) ‘ Dim str. Data(20) As String an array of 31 elements ' an array of 21 strings Dim two. Darray(10, 20) As Integer array of integers 'a two dimensional

ALLOCATING ONE-DIMENSIONAL ARRAY array_name=new Datatype(size){} Example: arr=new Integer(2){}

INITIALIZE ARRAY You can also initialize the array elements while declaring the array. Dim int. Data() As Integer = {12, 16, 20, 24, 28, 32} Dim names() As String = {“Ravi”, ”Ram”}

STORING DATA IN A ONEDIMENSIONAL ARRAY you use an assignment statement to enter data into an existing array Syntax: arrayname(subscript) = value Examples cities(0) = “Pune” cities(1) = “Nashik” cities(2) = “Mumbai”

Example: Program to store and display element in one dimensional array Module 1 Sub Main() 'Declaring and allocating the 1 D-array Dim arr() As Integer arr = New Integer(2) {1, 2, 3} 'Or 'arr = New Integer(2) {} 'arr(0) = 1 'arr(1) = 2 'arr(2) = 3 'Accessing the array elements For Each i In arr Console. Write. Line(i) Next Console. Read. Line() End Sub End Module

//Program to print sum of array elements Imports System Module 1 Sub Main() 'declaration of array Dim arr(2) As Integer arr(0) = 10 arr(1) = 20 arr(2) = 30 Dim sum As Integer = 0 For i As Integer = 1 To arr. Length - 1 sum = sum + arr(i) Next

'Print sum of array elements Console. Write. Line("SUM OF ALL ARRAY ELMENTS=" & sum) Console. Read. Key() End Sub End Module

Dynamic Array We can use the Dim statement to declare dynamic array without specifying the array size. Dynamic arrays can be dimensioned or re-dimensioned as we need using the Re. Dim statement. Re. Dim [Preserve] arrayname(subscripts) Where Preserve is a keyword used to preserve the data in an existing array, when we resize it and the subscripts is the new dimension of the array.

Example to demostrate Redim St Module 1 Sub Main() Dim num() As Integer Re. Dim num(2) num(0) = 1 num(1) = 2 Re. Dim Preserve num(5) num(2) = 3 num(3) = 4 num(4) = 5 num(5) = 6 For i = 0 To 5 Console. Write. Line(i & vb. Tab & num(i)) Next i Console. Read. Key() End Sub End Module

Another type of array is a 2 Dimensional array. This holds data in a grid pattern, rather like a spreadsheet. They look like this:

To set up a 2 -D array in Visual Basic. NET you just add a second number between the round brackets: Dim grid(3, 3) As Integer To store values in the grid, you need both numbers between the round brackets: grid(0, 0) = 1 This means fill row 0, column 0 with a value of 1. Example : 'Declare two dimensional array Dim two_dim (10, 5) As Integer 'Declare three dimensional array Dim three-dim (10, 20, 30) As Integer

Module arraymull Sub Main() ' an array with 5 rows and 2 columns Dim a(, ) As Integer = {{0, 0}, {1, 2}, {2, 4}, {3, 6}, {4, 8}} Dim i, j As Integer ' output each array element's value ' For i = 0 To 4 For j = 0 To 1 Console. Write. Line("a[{0}, {1}] = {2}", i, j, a(i, j)) Next j Next I Console. Read. Key() End Sub End Module

Jagged Array : -The array of arrays is called Jagged Array. Declaration Dim num As Integer()() = New Integer(10)(){} Example Module 1 Sub Main() 'This is a jagged array of 5 array of integers Dim a As Integer()() = New Integer(4)() {} a(0) = New Integer() {0, 0} a(1) = New Integer() {1, 2} a(2) = New Integer() {2, 4} a(3) = New Integer() {3, 6} a(4) = New Integer() {4, 8} Dim i, j As Integer ' output each array element's value For i = 0 To 4 For j = 0 To 1 Console. Write. Line("a[{0}, {1}] = {2}", i, j, a(i)(j)) Next j Next i Console. Read. Key() End Sub End Module
- Slides: 21