ITEC81: VB.NET Arrays (2) Lecture Notes PDF
Document Details
Uploaded by UltraCrispThorium6799
City College of Tagaytay
Rumer M. Bayot
Tags
Summary
This document is a lecture for a computer science class on VB.NET arrays. It covers various array properties and methods, including Length, Rank, GetLength, IsFixedSize, IsReadOnly, SyncRoot, GetValue, SetValue, IndexOf, LastIndexOf, Sort, Reverse, Clear, Copy, and Resize. Each method is explained, with code examples demonstrating usage.
Full Transcript
# ITEC81: VB.NET Arrays (2) ## Rumer M. Bayot, Instructor ## Presentation Outline 1. VB.NET Arrays Properties and Methods 2. Array Properties 3. Array Methods ## VB.NET Arrays Properties and Methods - In VB.NET, arrays are used to store multiple values in a single variable. - Arrays in VB.NET...
# ITEC81: VB.NET Arrays (2) ## Rumer M. Bayot, Instructor ## Presentation Outline 1. VB.NET Arrays Properties and Methods 2. Array Properties 3. Array Methods ## VB.NET Arrays Properties and Methods - In VB.NET, arrays are used to store multiple values in a single variable. - Arrays in VB.NET are objects, and they come with several properties and methods that allow you to manipulate and interact with the data they contain. - **Properties** are characteristics of the array. - **Methods** are functions that perform operations on the array. ## Array Properties ### Length - Retrieves the total number of elements in the array (all dimensions). - The Length property works for single-dimensional and multi-dimensional arrays. ```vb.net Dim numbers() As Integer = {10, 20, 30, 40, 50} Console.WriteLine("Total Elements: " & numbers.Length) Dim matrix(2, 3) As Integer ' A 3x4 matrix Console.WriteLine("Total Elements: " & matrix.Length) ``` ### Rank - Returns the number of dimensions (or "rank") of the array. ```vb.net Dim oneDim() As Integer = {10, 20, 30} Dim twoDim(2, 3) As Integer ' 2D array Dim threeDim(2, 2, 2) As Integer ' 3D array Console.WriteLine("Rank of oneDim: " & oneDim.Rank) Console.WriteLine("Rank of twoDim: " & twoDim.Rank) Console.WriteLine("Rank of threeDim: " & threeDim.Rank) ``` ### GetLength(dimensionIndex) - Returns the size of the specified dimension of a multi-dimensional array. - Use GetLength(0) for rows, GetLength(1) for columns, etc. ```vb.net Dim matrix(2, 3) As Integer ' 3 rows, 4 columns Console.WriteLine("Number of Rows: " & matrix.GetLength(0)) Console.WriteLine("Number of Columns: " & matrix.GetLength(1)) ``` ### IsFixedSize - Returns True if the array size is fixed (always True for VB.NET arrays). ```vb.net Dim numbers() As Integer = {10, 20, 30} Console.WriteLine("Is Fixed Size: " & numbers.IsFixedSize) ``` ### IsReadOnly - Returns True if the array is read-only (always False for VB.NET arrays). ```vb.net Dim numbers() As Integer = {10, 20, 30} Console.WriteLine("Is Read-Only: " & numbers.IsReadOnly) ``` ### SyncRoot and IsSynchronized - Used for thread synchronization. Not typically used in single-threaded applications. ```vb.net Dim numbers() As Integer = {10, 20, 30} Console.WriteLine("Is Synchronized: " & numbers.IsSynchronized) ``` ## Array Methods ### GetValue(index) - Retrieves the value stored at a specified index in the array. - Works for both single and multi-dimensional arrays. ```vb.net Dim numbers() As Integer = {10, 20, 30} Console.WriteLine("Value at Index 1: " & numbers.GetValue(1)) Dim matrix(1, 1) As Integer matrix(0, 0) = 10 Console.WriteLine("Value at (0,0): " & matrix.GetValue(0, 0)) ``` ### SetValue(value, index) - Sets a value at the specified index in the array. - Works for single and multi-dimensional arrays. ```vb.net Dim numbers() As Integer = {10, 20, 30} numbers.SetValue(50, 1) Console.WriteLine("Value at Index 1: " & numbers(1)) Dim matrix(1, 1) As Integer matrix.SetValue(100, 0, 1) Console.WriteLine("Value at (0,1): " & matrix(0, 1)) ``` ### IndexOf - Finds the first occurrence of a specified value and returns its index. - Returns -1 if the value is not found. ```vb.net Dim numbers() As Integer = {10, 20, 30, 20} Dim index As Integer = Array.IndexOf(numbers, 20) Console.WriteLine("Index of 20: " & index) ``` ### LastIndexOf - Finds the last occurrence of a specified value and returns its index. ```vb.net Dim numbers() As Integer = {10, 20, 30, 20} Dim index As Integer = Array.LastIndexOf(numbers, 20) Console.WriteLine("Last Index of 20: " & index) ``` ### Sort - Sorts the array in ascending order. - Can sort arrays of numbers, strings, or objects (requires a comparer for objects). ```vb.net Dim numbers() As Integer = {30, 10, 20} Array.Sort(numbers) Console.WriteLine("Sorted Array: " & String.Join(", ", numbers)) ``` ### Reverse - Reverses the order of elements in the array. ```vb.net Dim numbers() As Integer = {10, 20, 30} Array.Reverse(numbers) Console.WriteLine("Reversed Array: " & String.Join(", ", numbers)) ``` ### Clear - Sets all elements in the array to their default values (e.g., 0 for integers, Nothing for objects). ```vb.net Dim numbers() As Integer = {10, 20, 30} Array.Clear(numbers, 0, numbers.Length) Console.WriteLine("Cleared Array: " & String.Join(", ", numbers)) ``` ### Copy - Copies elements from one array to another. ```vb.net Dim source() As Integer = {10, 20, 30} Dim destination(2) As Integer Array.Copy(source, destination, 3) Console.WriteLine("Copied Array: " & String.Join(", ", destination)) ``` ### Resize - Resizes the array to the specified size. If the new size is larger, new elements are initialized to their default values. ```vb.net Dim numbers() As Integer = {10, 20, 30} Array.Resize(numbers, 5) numbers(3) = 40 numbers(4) = 50 Console.WriteLine("Resized Array: " & String.Join(", ", numbers)) ``` ### BinarySearch - Searches for a value in a sorted array using the binary search algorithm. ```vb.net Dim numbers() As Integer = {10, 20, 30, 40, 50} Console.WriteLine("Index of 30: " & Array.BinarySearch(numbers, 30)) ``` ### Find - Finds the first element that matches a specified condition. ```vb.net Dim numbers() As Integer = {10, 20, 30} Dim result As Integer = Array.Find(numbers, Function(x) x > 15) Console.WriteLine("First Element > 15: " & result) ``` ### FindAll - Finds all elements that match a specified condition. ```vb.net Dim numbers() As Integer = {10, 20, 30, 40} Dim results() As Integer = Array.FindAll(numbers, Function(x) x > 20) Console.WriteLine("Elements > 20: " & String.Join(", ", results)) ``` ## Questions? The image contains an illustration of a desk with a computer and a lamp. The question mark is colored with the colors of Google: red, yellow and blue.