Structure WeightElement 'The Weights are stored in an array of Weight Elemenets, which are used as a tree
Dim LowPointer As Integer
Dim HighPointer As Integer
Dim TraitPointer As Integer 'This pointer allows another tree to be appended to certain elements, for the sake of dependent traits
Dim Values As WeightedTrait()
Dim Num As Integer 'The Weight Elements are named by numbers for ease of comparison
End Structure
Structure WeightedTrait
Dim TraitName As String 'This is the value that is displayed to the user when a character is made
Dim TraitNum As Integer 'The trait number is used to find the correct element in trait sub-trees
Dim WeightValue As Decimal
End Structure
Function ChewQuery(QueryList As String)
'This function takes in a string and partitions it into a weight tree
Dim ElementList() As String
ElementList = QueryList.Split(";")
Dim LoadedWeight(ElementList.Length) As WeightElement
For x = 0 To ElementList.Length - 1
'Using this method, in which the front portion of the query is repeatedly removed, allows for a simpler query structure, so that we don't need to partition between the pointers and values
LoadedWeight(x).LowPointer = CInt(BiteQuery(ElementList(x)))
LoadedWeight(x).HighPointer = CInt(BiteQuery(ElementList(x)))
LoadedWeight(x).TraitPointer = CInt(BiteQuery(ElementList(x)))
LoadedWeight(x).Num = CInt(BiteQuery(ElementList(x)))
ChewQueryValues(ElementList(x), LoadedWeight(x))
Next
Return LoadedWeight
End Function
Function BiteQuery(ByRef QueryList As String)
Dim Marker As Integer
Dim Bite As String
'This function partitions the input string around the first comma
'It returns the section before the comma, and stores the section behind the comma as the new value for the string
Try
Marker = InStr(QueryList, ",")
Bite = Left(QueryList, Marker - 1)
Marker = Len(QueryList) - Marker
QueryList = Right(QueryList, Marker)
Catch
'This is used in the case that a list without a comma is input
Bite = QueryList
QueryList = ""
End Try
Return Bite
End Function
Sub ChewQueryValues(ByRef QueryList As String, ByRef LoadedWeight As WeightElement)
LoadedWeight.Values = {}
While Len(QueryList) > 0
'This While loop is so that an arbitrary number of values can be inserted
'Because BiteQuery takes in functions by reference, each loop reduces the length of the string until it is empty
ReDim Preserve LoadedWeight.Values(LoadedWeight.Values.Length + 1)
LoadedWeight.Values(LoadedWeight.Values.Length - 1).TraitName = BiteQuery(QueryList)
LoadedWeight.Values(LoadedWeight.Values.Length - 1).TraitNum = CInt(BiteQuery(QueryList))
LoadedWeight.Values(LoadedWeight.Values.Length - 1).WeightValue = CDec(BiteQuery(QueryList))
End While
End Sub
These subroutines should transform a properly formatted string into a WeightElement array. The string I am using is:
1,2,0,5,Black,26,1,White,30,1,EastAsian,27,1,SouthAsian,28,1,MiddleEastern,29,1;3,4,25,3,N/A,0,1;28,5,6,13,N/A,0,1;-1,7,0,1,Male,16,1,Female,17,1,Non-Binary,18,1;-1,-1,8,4,N/A,0,1;-1,34,9,15,N/A,0,1;10,11,0,28,Light,-1,1,Medium,-1,1;-1,-1,12,2,N/A,0,1;13,14,0,21,Andro,19,1,Gyno,20,1,A,21,1,Bi,22,1,Pan,23,1;15,16,0,28,Straight,-1,1,Wavy,-1,1,Curly,-1,1;-1,17,0,26,Light,-1,1,Medium,-1,1,Dark,-1,1;18,-1,0,30,Pale,-1,1,Light,-1,1;19,20,0,17,Male,31,1,Female,32,1,Intersex,33,1;-1,21,0,19,Andro,19,1,Gyno,20,1,A,21,1,Bi,22,1,Pan,23,1;22,-1,0,23,Andro,19,1,Gyno,20,1,A,21,1,Bi,22,1,Pan,23,1;-1,23,0,26,Curly,-1,1,Coily,-1,1;24,-1,0,30,Straight,-1,1,Wavy,-1,1,Curly,-1,1;-1,-1,0,27,Pale,-1,1,Light,-1,1,Medium,-1,1;-1,-1,0,29,Pale,-1,1,Light,-1,1,Medium,-1,1;-1,-1,0,16,Male,31,1,Female,32,1,Intersex,33,1;-1,-1,0,18,Male,31,1,Female,32,1,Intersex,33,1;-1,-1,0,20,Andro,19,1,Gyno,20,1,A,21,1,Bi,22,1,Pan,23,1;-1,-1,0,22,Andro,19,1,Gyno,20,1,A,21,1,Bi,22,1,Pan,23,1;-1,-1,0,27,Straight,-1,1,Wavy,-1,1;-1,-1,0,29,Straight,-1,1,Wavy,-1,1,Curly,-1,1;26,27,0,17,Andro,19,1,Gyno,20,1,A,21,1,Bi,22,1,Pan,23,1;-1,-1,0,16,Andro,19,1,Gyno,20,1,A,21,1,Bi,22,1,Pan,23,1;-1,-1,0,18,Andro,19,1,Gyno,20,1,A,21,1,Bi,22,1,Pan,23,1;29,30,0,8,Albinism,-1,1,ChronicFatigue,-1,1,Dwarfism,-1,1;-1,31,0,6,Autistic,-1,1,ADHD,-1,1,OCD,-1,1,Tourrette,-1,1,Epilepsy,-1,1;32,33,35,10,N/A,0,1;-1,-1,0,7,OneArmAbsent,-1,1,BothArmsAbsent,-1,1,OneHandwithAnomalies,-1,1,BothHandswithAnomalies,-1,1,OneLegAbsent,26,1,BothLegsAbsent,26,1;-1,-1,36,9,N/A,0,1;-1,-1,0,11,GoodHearing,-1,1,HardofHearing,-1,1,Deaf,-1,1,NoHearing,-1,1;-1,-1,0,34,Scars,-1,1,NaturalAnomalies,-1,1,OneEyeAbsent,-1,1,BothEyesAbsent,-1,1;-1,37,0,0,GoodVision,-1,1,Blind,-1,1,NoVision,-1,1;-1,38,0,0,Unaided,-1,1,Cane,-1,1,Wheelchair,-1,1,Full-TimeWheelchair,-1,1;-1,-1,0,25,N/A,0,1;-1,-1,0,26,Unaided,-1,1,Cane,-1,1,Wheelchair,-1,1,Full-TimeWheelchair,-1,1
The format is that the first four values between each semicolon are the low pointer, high pointer, trait pointer, and Number, in that order. After this point are the values, with the trait name, trait number, and weight value, repeating in that order
When I use this string, the program decides to return a WeightElement array of the correct length, but where all the values are 0 or empty strings. How do I make it produce a proper WeightElement, as by the format above?
How can I delete an item from an array in VB.NET?
As Heinzi said, an array has a fixed size. In order to 'remove an item' or 'resize' it, you'll have to create a new array with the desired size and copy the items you need as appropriate.
Here's code to remove an item from an array:
<System.Runtime.CompilerServices.Extension()> _
Function RemoveAt(Of T)(ByVal arr As T(), ByVal index As Integer) As T()
Dim uBound = arr.GetUpperBound(0)
Dim lBound = arr.GetLowerBound(0)
Dim arrLen = uBound - lBound
If index < lBound OrElse index > uBound Then
Throw New ArgumentOutOfRangeException( _
String.Format("Index must be from {0} to {1}.", lBound, uBound))
Else
'create an array 1 element less than the input array
Dim outArr(arrLen - 1) As T
'copy the first part of the input array
Array.Copy(arr, 0, outArr, 0, index)
'then copy the second part of the input array
Array.Copy(arr, index + 1, outArr, index, uBound - index)
Return outArr
End If
End Function
You can use it as such:
Module Module1
Sub Main()
Dim arr = New String() {"abc", "mno", "xyz"}
arr.RemoveAt(1)
End Sub
End Module
The code above removes the second element ("mno") [which has an index of 1] from the array.
You need to be developing in .NET 3.5 or higher in order to use the extension method.
If you're using .NET 2.0 or 3.0, you can call the method as such
arr = RemoveAt(arr, 1)
I hope this is what you need.
Update
After running tests based on ToolMakerSteve's comment it appears the initial code does not modify the array you want to update because of the ByVal used in the function's declaration. However, writing code like arr = arr.RemoveAt(1) or arr = RemoveAt(arr, 1) does modify the array because it reassigns the modified array to the original.
Find below the updated method (subroutine) for removing an element from an array.
<System.Runtime.CompilerServices.Extension()> _
Public Sub RemoveAt(Of T)(ByRef arr As T(), ByVal index As Integer)
Dim uBound = arr.GetUpperBound(0)
Dim lBound = arr.GetLowerBound(0)
Dim arrLen = uBound - lBound
If index < lBound OrElse index > uBound Then
Throw New ArgumentOutOfRangeException( _
String.Format("Index must be from {0} to {1}.", lBound, uBound))
Else
'create an array 1 element less than the input array
Dim outArr(arrLen - 1) As T
'copy the first part of the input array
Array.Copy(arr, 0, outArr, 0, index)
'then copy the second part of the input array
Array.Copy(arr, index + 1, outArr, index, uBound - index)
arr = outArr
End If
End Sub
Usage of the method is similar to the original, except there is no return value this time so trying to assign an array from the return value will not work because nothing is returned.
Dim arr = New String() {"abc", "mno", "xyz"}
arr.RemoveAt(1) ' Output: {"abc", "mno"} (works on .NET 3.5 and higher)
RemoveAt(arr, 1) ' Output: {"abc", "mno"} (works on all versions of .NET fx)
arr = arr.RemoveAt(1) 'will not work; no return value
arr = RemoveAt(arr, 1) 'will not work; no return value
Note:
I use a temporary array for the process because it makes my intentions clear and that is exactly what VB.NET does behind the scenes when you do Redim Preserve. If you would like to modify the array in-place using Redim Preserve, see ToolmakerSteve's answer.
The RemoveAt methods written here are extension methods. In order for them to work, you will have to paste them in a Module. Extension methods will not work in VB.NET if they are placed in a Class.
Important If you will be modifying your array with lots of 'removes', it is highly recommended to use a different data structure such as List(Of T) as suggested by other answerers to this question.
You can't. I would suggest that you put the array elements into a List, at least then you can remove items. An array can be extended, for example using ReDim but you cannot remove array elements once they have been created. You would have to rebuild the array from scratch to do that.
If you can avoid it, don't use arrays here, use a List.
One line using LINQ:
Dim arr() As String = {"uno", "dos", "tres", "cuatro", "cinco"}
Dim indx As Integer = 2
arr = arr.Where(Function(item, index) index <> indx).ToArray 'arr = {"uno", "dos", "cuatro", "cinco"}
Remove first element:
arr = arr.Skip(1).ToArray
Remove last element:
arr = arr.Take(arr.length - 1).ToArray
That depends on what you mean by delete. An array has a fixed size, so deleting doesn't really make sense.
If you want to remove element i, one option would be to move all elements j > i one position to the left (a[j - 1] = a[j] for all j, or using Array.Copy) and then resize the array using ReDim Preserve.
So, unless you are forced to use an array by some external constraint, consider using a data structure more suitable for adding and removing items. List<T>, for example, also uses an array internally but takes care of all the resizing issues itself: For removing items, it uses the algorithm mentioned above (without the ReDim), which is why List<T>.RemoveAt is an O(n) operation.
There's a whole lot of different collection classes in the System.Collections.Generic namespace, optimized for different use cases. If removing items frequently is a requirement, there are lots of better options than an array (or even List<T>).
Yes, you can delete an element from an array. Here is an extension method that moves the elements as needed, then resizes the array one shorter:
' Remove element at index "index". Result is one element shorter.
' Similar to List.RemoveAt, but for arrays.
<System.Runtime.CompilerServices.Extension()> _
Public Sub RemoveAt(Of T)(ByRef a() As T, ByVal index As Integer)
' Move elements after "index" down 1 position.
Array.Copy(a, index + 1, a, index, UBound(a) - index)
' Shorten by 1 element.
ReDim Preserve a(UBound(a) - 1)
End Sub
Usage examples (assuming array starting with index 0):
Dim a() As String = {"Albert", "Betty", "Carlos", "David"}
a.RemoveAt(0) ' Remove first element => {"Betty", "Carlos", "David"}
a.RemoveAt(1) ' Remove second element => {"Betty", "David"}
a.RemoveAt(UBound(a)) ' Remove last element => {"Betty"}
Removing First or Last element is common, so here are convenience routines for doing so (I like code that expresses my intent more readably):
<System.Runtime.CompilerServices.Extension()> _
Public Sub DropFirstElement(Of T)(ByRef a() As T)
a.RemoveAt(0)
End Sub
<System.Runtime.CompilerServices.Extension()> _
Public Sub DropLastElement(Of T)(ByRef a() As T)
a.RemoveAt(UBound(a))
End Sub
Usage:
a.DropFirstElement()
a.DropLastElement()
And as Heinzi said, if you find yourself doing this, instead use List(Of T), if possible. List already has "RemoveAt" subroutine, and other routines useful for inserting/deleting elements.
My favorite way:
Imports System.Runtime.CompilerServices
<Extension()> _
Public Sub RemoveAll(Of T)(ByRef arr As T(), matching As Predicate(Of T))
If Not IsNothing(arr) Then
If arr.Count > 0 Then
Dim ls As List(Of T) = arr.ToList
ls.RemoveAll(matching)
arr = ls.ToArray
End If
End If
End Sub
Then in the code, whenever I need to remove something from an array I can do it by some property in some object in that array having a certain value, like:
arr.RemoveAll(Function(c) c.MasterContactID.Equals(customer.MasterContactID))
Or if I already know the exact object I want to remove, I can just do:
arr.RemoveAll(function(c) c.equals(customer))
The variable i represents the index of the element you want to delete:
System.Array.Clear(ArrayName, i, 1)
This may be a lazy man's solution, but can't you just delete the contents of the index you want removed by reassigning their values to 0 or "" and then ignore/skip these empty array elements instead of recreating and copying arrays on and off?
Public Sub ArrayDelAt(ByRef x As Array, ByVal stack As Integer)
For i = 0 To x.Length - 2
If i >= stack Then
x(i) = x(i + 1)
x(x.Length-1) = Nothing
End If
Next
End Sub
try this
Seems like this sounds more complicated than it is...
Dim myArray As String() = TextBox1.Lines
'First we count how many null elements there are...
Dim Counter As Integer = 0
For x = 0 To myArray.Count - 1
If Len(myArray(x)) < 1 Then
Counter += 1
End If
Next
'Then we dimension an array to be the size of the last array
'minus the amount of nulls found...
Dim tempArr(myArray.Count - Counter) As String
'Indexing starts at zero, so let's set the stage for that...
Counter = -1
For x = 0 To myArray.Count - 1
'Set the conditions for the new array as in
'It .contains("word"), has no value, length is less than 1, ect.
If Len(myArray(x)) > 1 Then
Counter += 1
'So if a value is present, we move that value over to
'the new array.
tempArr(Counter) = myArray(x)
End If
Next
Now you can assign tempArr back to the original or what ever you need done with it as in...
TextBox1.Lines = tempArr (You now have a textbox void of blank lines)
If the array is a string array you are able to then do the following:
AlphaSplit = "a\b\c".Split("\")
MaxIndex = AlphaSplit.GetUpperBound(0)
AlphaSplit = AlphaSplit.Where(Function(item, index) index <> MaxIndex).ToArray
AlphaJoin = String.Join("\", PublishRouteSplit)
How about this method:
Get a method which return an array, say tempArray
tempArray is supposed to have at least 1 less element to your array, say permArray
The method should take and integer param (this will be the index of the unwanted element) say ommitIndex and your permArray
In the method, copy all elements excluding the element as position ommitIndex from permArray to tempArray
The method returns tempArray so update permArray with the method.
Here's a snippet
Function updateArray(ommitIndex As Integer, array() As String) As Array
Dim tempArray(array.Length - 2) As String
Dim counter As Integer = 0
For i As Integer = 0 To (array.Length - 1)
If (i <> ommitIndex) Then
tempArray(counter) = array(i)
counter += 1
End If
Next
Return tempArray
End Function