Array

Introduction

  • An array in VB .NET is a simple and static data structure.

Definition

  • In VB .NET, an Array in VB .NET is a collection of elements of the same data type stored in contiguous memory locations and accessed using a single variable name with an index. However, if an array is declared as an Object array, it can store values of different data types, because Object is the base class of all data types in .NET. Object array is useful when data types are not known in advance.

Characteristics

  • Arrays in VB .NET provide an efficient way to store and manipulate multiple values efficiently and process them using loops of the same data type.
  • They are widely used in programming for handling lists of data and performing repetitive operations.
  • Array elements are accessed using index numbers, which start from 0.
  • Arrays have a fixed size once declared.

Types

  • Arrays can be
    • One-dimensional Array
    • Two-dimensional Array
    • Multidimensional Array.
    • Jagged Array
  • Declaring a one-dimensional Array in VB .NET
    • A one-dimensional array stores data in a single row.
Syntax: Dim arrayName(size) As DataType
For ExampleDim numbers(4) As Integer
This creates an array with 5 elements (0 to 4).
Initializing one dimensional Array
Dim marks() As Integer = {75, 80, 90, 85}
Accessing Array Elements
Console.WriteLine(marks(2))      ‘ Outputs 90
Assigning values in Array Elements
Dim x(2) As Integer
x(0) = 100
x(1) = 200
x(2) = 300
Printing an array of values
For i As Integer = 0 To x.Length – 1
    Console.WriteLine(x(i))
Next
    • Two-Dimensional Array
      • This array stores data in rows and columns.
      • For example – Dim x(1, 2) As Integer
x(0,0) = 100
x(0,1) = 200
    • Jagged Array
      • An array of arrays where each row can have a different size.
Dim y()() As Integer = {
    New Integer() {1, 2},
    New Integer() {3, 4, 5}
}

Array Properties and Methods

  • Length – Returns the total number of elements
  • GetLength(dimension) – Returns the length of a specific dimension
  • Array.Sort(arrayName)
  • Array.Reverse(arrayName)

Advantages of Arrays

  • An array is a simple and efficient data storage.
  • An array supports easy data manipulation using loops.
  • An array has reduced code complexity.

Limitations of Arrays

  • An array has a fixed size.
  • An array can store only one data type.
  • In an array, insertion and deletion are difficult.

Collection

  • A collection in VB.NET is a dynamic data structure used to store and manage a group of related data items or objects efficiently, allowing them to be added, removed, and accessed dynamically at runtime, and is accessed using keys or indexes.
  • Collections are part of the System. Collections and System.Collections.Generic namespaces.
  • Characteristics of Collections in VB .NET
    • Collections can store multiple elements in a single object.
    • Unlike arrays, the size of a collection is dynamic.
    • Elements can be accessed using index numbers or keys.
    • Collections provide several built-in methods, such as Add, Remove, and Count, to perform specific operations easily.
  • Example
Dim students As New Collection

students.Add(“Amit”)
students.Add(“Ravi”)
students.Add(“Suman”)

For Each name As String In students
    Console.WriteLine(name)
Next

Output
Amit
Ravi
Suman
  • Types of Collections in VB .NET
1. Non-Generic Collections

Collection
ArrayList
Hashtable
Queue
Stack

2. Generic Collections (Type-Safe)

List
Dictionary
Queue
Stack

Example of a Generic Collection
Dim marks As New List(Of Integer)

marks.Add(85)
marks.Add(90)
marks.Add(78)

Console.WriteLine(marks.Count)



Loading


0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.