Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,709 questions

55,473 answers

573 users

How to build sparse array in VB.NET

2 Answers

0 votes
Imports System
Imports System.Collections.Generic

'
'   A small sparse array representation:
'   - Each entry stores an index and a value
'   - Zero values are simply not included
'

Public Class SparseEntry
    Public Property Index As Integer
    Public Property Value As Integer

    Public Sub New(i As Integer, v As Integer)
        Index = i
        Value = v
    End Sub
End Class

Module SparseToDense

    '
    '   buildDense:
    '   Converts sparse entries into a dense array.
    '
    '   Steps:
    '   1. Find the largest index
    '   2. Allocate an array of size maxIndex + 1
    '   3. Fill with zeros (VB.NET does this automatically)
    '   4. Copy sparse values into their positions
    '
    Function BuildDense(sa As List(Of SparseEntry)) As Integer()
        Dim maxIndex As Integer = 0

        ' Find largest index
        For Each e In sa
            If e.Index > maxIndex Then
                maxIndex = e.Index
            End If
        Next

        ' Allocate dense array filled with zeros
        Dim dense(maxIndex) As Integer

        ' Copy sparse values
        For Each e In sa
            dense(e.Index) = e.Value
        Next

        Return dense
    End Function

    Sub Main()
        ' Sparse entries (zero values omitted)
        Dim sa As New List(Of SparseEntry) From {
            New SparseEntry(2, 10),
            New SparseEntry(10, 7),
            New SparseEntry(8, 42),
            New SparseEntry(3, 5)
        }

        Dim dense() As Integer = BuildDense(sa)

        Console.WriteLine("Dense array:")
        Console.Write("[ ")
        For Each v In dense
            Console.Write(v & " ")
        Next
        Console.WriteLine("]")
    End Sub

End Module


'
' run:
'
' Dense array:
' [ 0 0 10 5 0 0 0 0 42 0 7 ]
'

 



answered 9 hours ago by avibootz
0 votes
Imports System
Imports System.Collections.Generic

'
' A sparse array stores only non‑zero values.
' Dictionary(Of Integer, Integer) is a natural fit:
'   - Keys represent indices that actually exist
'   - Values represent stored data
'   - Lookup and insertion are fast
'

Module SparseArrayDemo

    '
    ' buildDense:
    ' Converts sparse → dense.
    '
    ' Steps:
    ' 1. Find the maximum index in the sparse structure
    ' 2. Allocate a dense array of size maxIndex + 1
    ' 3. Fill with zeros (VB.NET does this automatically)
    ' 4. Copy sparse values into their positions
    '
    Function BuildDense(sa As Dictionary(Of Integer, Integer)) As Integer()
        Dim maxIndex As Integer = 0

        ' Find largest index
        For Each kvp In sa
            If kvp.Key > maxIndex Then
                maxIndex = kvp.Key
            End If
        Next

        ' Allocate dense array
        Dim dense(maxIndex) As Integer

        ' Copy sparse values
        For Each kvp In sa
            dense(kvp.Key) = kvp.Value
        Next

        Return dense
    End Function

    Sub Main()
        '
        ' Sparse entries (zero values omitted)
        '
        Dim sa As New Dictionary(Of Integer, Integer) From {
            {2, 10},
            {10, 7},
            {8, 42},
            {3, 5}
        }

        Dim dense() As Integer = BuildDense(sa)

        Console.WriteLine("Dense array:")
        Console.Write("[ ")
        For Each v In dense
            Console.Write(v & " ")
        Next
        Console.WriteLine("]")
    End Sub

End Module


' run:

' Dense array:
' [ 0 0 10 5 0 0 0 0 42 0 7 ]
' 

 



answered 9 hours ago by avibootz
...