Using Arrays and Collections

Dim prices(4) declares indexes 0 to 4, FIVE elements, કારણ કે VB.NET arrays upper bound દ્વારા declared થાય છે; ReDim Preserve તેમને grows કરે છે, અને ArrayList પોતે grows થાય છે.

10 min read · 9 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

કેટલા items bill પર fit થાય છે?

ShopKeeper ની first bill design array ને use કરે છે: એક purchase ના items માટે space.

Dim items(9) As String

Customer number 3 એ 11 things ને buy કરે છે.

તમે આ wall ને twice before meet કર્યું છે: BCA104 ના C arrays, અને BCA403 ના Unit 5 hook, જ્યાં તેણે આપણને linked lists build કરવા push કર્યા. VB.NET આજે 2 gentler answers offer કરે છે: array જે re-dimensioned થઈ શકે છે, અને collection જે પોતે grows થાય છે. પણ first, VB array surprise જે દરેક Java-trained eye ને catch કરે છે.

Theory

Declared by upper bound, count દ્વારા નહીં

આ line ને fresh eyes સાથે read કરો:

Dim prices(4) As Double

C અથવા Java માં, 4 એટલે 4 elements હોત. VB.NET માં તે highest index છે: array prices(0) થી prices(4) સુધી runs કરે છે, જે 5 elements છે, અને prices.Length 5 કહે છે.

Access parentheses ને use કરે છે, brackets નહીં: prices(0) = 40. બંને facts pure syntax છે, અને બંને almost દરેક BCA404 paper માં mark apiece worth છે.

Practical

Arrays, ReDim, અને self-growing ArrayList

Imports System.Collections

Module BillDemo
    Sub Main()
        Dim prices(2) As Double        ' indexes 0..2: THREE slots
        prices(0) = 40 : prices(1) = 100 : prices(2) = 60
        MsgBox(prices.Length)          ' 3

        ReDim Preserve prices(4)       ' 5 slots માં grow, 3 values ને KEEP
        prices(3) = 25

        Dim items As New ArrayList()   ' પોતે grows થાય છે
        items.Add("Soap")
        items.Add("Tea")
        items.Add("Rice")
        items.Remove("Tea")
        MsgBox(items.Count)            ' 2

        For Each thing As Object In items
            MsgBox(CStr(thing))        ' Soap, પછી Rice
        Next
    End Sub
End Module

Quiz

Dim qty(4) As Integer. આ array કેટલા elements ને hold કરે છે?

  1. 4, indexes 1 to 4
  2. 4, indexes 0 to 3
  3. 5, indexes 0 to 4
  4. તે Option Strict પર depend કરે છે
Show the answer

5, indexes 0 to 4

VB.NET arrays ને UPPER BOUND દ્વારા declare કરે છે: qty 0 થી 4 સુધી runs કરે છે, five elements, Length 5. Option B એ Java/C reading છે same line ની, exact habit જે આ question hunt કરે છે: those languages માં new int[4] નો અર્થ 4 elements હતો. Option A 1-based arrays ને invent કરે છે, જે VB.NET પાસે નથી (તેના arrays 0 થી start થાય છે, ફક્ત તેના classic STRING functions 1 થી count કરે છે: last lesson). Option D unrelated compiler setting ને drag કરે છે; Option Strict conversions ને govern કરે છે, ક્યારેય array sizing નહીં.

Think first

ReDim vs ReDim Preserve

bill array 40, 100, 60 ને hold કરે છે. એક student ReDim prices(4) લખે છે, બીજો ReDim Preserve prices(4) લખે છે. tap કરતા પહેલા: દરેક array પછી શું contain કરે છે?

Show the answer

Plain ReDim prices(4) fresh 5-slot array ને allocate કરે છે: all zeros; 40, 100, 60 GONE છે. ReDim Preserve prices(4) 5 slots ને allocate કરે છે અને old values ને across copy કરે છે: 40, 100, 60, 0, 0. એક keyword, આખી bill saved અથવા wiped. Preserve full copy ને cost કરે છે, એટલે loop માં એક slot at a time grow કરવું wasteful છે, અને તે hint (repeated unpredictable growth) exactly signal છે arrays ને resize કરવાનું stop કરવા અને collection ને use કરવા.

Theory

ArrayList: bag જે grows થાય છે

Dim items As New ArrayList() (System.Collections થી) ને ક્યારેય size ની જરૂર નથી:

  • items.Add("Soap") append કરે છે; items.Remove("Tea") value દ્વારા delete કરે છે; items.RemoveAt(0) position દ્વારા
  • items.Count એ live size છે; items(0) index દ્વારા read કરે છે

એક honest cost: ArrayList everything ને As Object store કરે છે. Strings references તરીકે slide in થાય છે, પણ Integer ને add કરવું તેને box કરે છે (boxing lesson, તેનો cheque cash કરે છે), અને તેને back read કરવા માટે CInt ની જરૂર પડે છે. typed modern cousin, List(Of Double), boxing ને skip કરે છે અને wrong types ને compile time પર refuse કરે છે.

Watch out

ત્રણ array slips જે avoid કરવા

Brackets: prices[0] એ VB નથી; parentheses only. (confusingly, same parentheses functions ને પણ call કરે છે: context decide કરે છે.)

Off the end: (4)-declared array પર prices(5) IndexOutOfRangeException throw કરે છે run time પર; compiler તમને save નથી કરતું.

Plain ReDim keep-the-data situation માં: silent, compiles, array ને wipe કરે છે. જો values matter કરે છે, Preserve word matter કરે છે.

Theory

કયું container, ક્યારે

shopkeeper જેમ decide કરો: fixed rack sizes જે તમે advance માં જાણો છો (7 weekday totals) array ને suit કરે છે; bill જેની length customer પર depend કરે છે તે ArrayList (અથવા List(Of T)) ને suit કરે છે. ShopKeeper નું bill બીજું kind છે, અને Unit 3 થી onward તેના items collection માં live થશે જે DataGrid ને feed કરે છે. અને BCA403 ના Unit 5 ને યાદ રાખો: linked lists એ same problem ને BY HAND solve કર્યું હતું; collections એ that idea છે, pre-built અને shelf-bought.

Summary

Key takeaways

  • Dim a(4) As Integer UPPER BOUND ને declare કરે છે: indexes 0..4, 5 elements, Length 5.
  • VB arrays elements માટે parentheses ને use કરે છે: a(0); out-of-range access run time પર fail થાય છે.
  • ReDim resizes પણ wipes કરે છે; ReDim Preserve resizes અને data ને keep કરે છે (copy ના cost પર).
  • ArrayList પોતે grows અને shrinks થાય છે: Add, Remove, RemoveAt, Count.
  • ArrayList Objects ને store કરે છે: value types get boxed; List(Of T) એ typed, box-free alternative છે.
  • For Each arrays અને collections બંને ને walk કરે છે.
  • Memory hook: brackets માં number એ top index છે, head-count નહીં.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Programming in Visual Basic .NET

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Using Arrays and Collections · .NET Programming · Gri-Learn